Search code examples
androidgoogle-apiaccountmanagerandroid-youtube-apigoogle-authentication

GoogleAuthToken returns Access Token without access permission from scope and throw error usuage Limits


In android, I have given scope and called GoogleAuthToken as below

String scope = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com";
String accessToken = GoogleAuthUtil.getToken(mContext, emailID, scope);

I am receiving accessToken. but I cannot use the access token to use in youtube api access.

I tried like this

    HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    JacksonFactory JSON_FACTORY = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential.Builder()
        .setJsonFactory(JSON_FACTORY)
        .setTransport(HTTP_TRANSPORT)
        .build();

    credential.setAccessToken(accessToken);

    YouTube youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
        .setApplicationName("youtube-cmdline-listbroadcasts-sample").build();
    YouTube.LiveBroadcasts.List liveBroadcastRequest =  youtube.liveBroadcasts().list("id");
    liveBroadcastRequest.setBroadcastStatus("all");
    liveBroadcastRequest.setMaxResults((long) 1);
    liveBroadcastRequest.execute();

liveBroadcastRequest.execute() is giving error as

 com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
  {
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",

    "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project.",
    "reason" : "accessNotConfigured"
  } ],
 "message" : "Access Not Configured. Please use Google Developers Console to activate the API for your project."
}

Sometimes GoogleAuthToken throws exception and I am getting and called the below

if (e instanceof UserRecoverableAuthException) {
                // Unable to authenticate, such as when the user has not yet granted
                // the app access to the account, but the user can fix this.
                // Forward the user to an activity in Google Play services.
                Intent intent = ((UserRecoverableAuthException)e).getIntent();
                ((Activity)context).startActivityForResult(intent,REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
            }

It will ask permission. after It is getting end, I will get access token as once again I call first line code. but same error is repeating.

thanks in advance


Solution

  • Yes, I solve the problem in Android

    This is due to API access given to the application. For each developer machine SHA1 should be given to API and create separate Android API key for the each SHA1.

    1. Go to console.developers.google.com -> select your project
    2. API & AUTH -> credentials
    3. Create new client Key (though it has other client keys)
    4. select installed app -> android
    5. Give your package name and SHA1 correctly select OK

    Your problem will be automatically solved.