How to properly authenticate the Flutter GoogleAPIs package for AdMob

187

Alright guys, it turns out that, after weeks of trying to fix this, the only problem was my own stupidity.

To test this program I was signing in to a Gmail account which did not have an admob account set up. All you need to do is go to admob and setup your account with the email you want to use. Then everything works as intended.

To all the future idiots, don't make the same mistake as me.

Share:
187
Arthur Facredyn
Author by

Arthur Facredyn

Updated on December 30, 2022

Comments

  • Arthur Facredyn
    Arthur Facredyn over 1 year

    I have downloaded the example for using the flutter googleapis package where usage of the PeopleAPI is shown, this example.

    I configured my project on the google cloud platform and did every thing necessary to make the example work, which it did.

    I then went ahead and tried to make the project work with the YoutubeAPI (v3). I added the scopes to the OAuth consent screen (on the google cloud platform), and also inside of my code.

    final GoogleSignIn _googleSignIn = GoogleSignIn(
      scopes: <String>[
        PeopleServiceApi.contactsReadonlyScope,
        YouTubeApi.youtubeReadonlyScope,
      ],
    );
    

    This worked perfectly and I was able to use the YouTube API effectively.

    I now want to use the AdmobAPI (v1). I added the necessary scopes to the OAuth consent screen (on the google cloud platform), and also inside of my code.

    final GoogleSignIn _googleSignIn = GoogleSignIn(
      scopes: <String>[
        PeopleServiceApi.contactsReadonlyScope,
        YouTubeApi.youtubeReadonlyScope,
        AdMobApi.admobReadonlyScope,
        AdMobApi.admobReportScope
      ],
    );
    

    I then tried a basic example usage of the AdmobAPI. This does not work even though the setup process is exactly the same as for the Youtube API. Both APIs are enabled on the google cloud console.

    Here is the error output by the AdmobAPI

    [VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: DetailedApiRequestError(status: 401, message: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.)
    #0      _validateResponse (package:_discoveryapis_commons/src/api_requester.dart:295:9)
    <asynchronous suspension>
    #1      ApiRequester.request (package:_discoveryapis_commons/src/api_requester.dart:73:16)
    <asynchronous suspension>
    #2      AccountsResource.list (package:googleapis/admob/v1.dart:152:23)
    <asynchronous suspension>
    #3      SignInDemoState._handleGetContact (package:admob_revenue/main.dart:76:11)
    <asynchronous suspension>
    

    The exact same authenticated client is used in both APIs This app is being run on an iOS simulator

    And here is the code I am using to produce this error: https://pastebin.com/3uw4EtMS See lines 62 - 76 for where I am calling the APIs

    So essentially my question is, what am I doing wrong that makes the YouTubeAPI authentication work, but not the AdMobAPI authentication?

    I was not able to find any docs saying that the authentication process would be different for these API's other than a change in scopes.