GoogleAuthToken在没有范围访问权限的情况下返回访问令牌,并抛出错误使用限制

时间:2014-07-08 14:01:32

标签: android google-api accountmanager android-youtube-api google-authentication

在android中,我已经给出了范围,并在下面调用了GoogleAuthToken

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

我正在接收accessToken。但我无法使用访问令牌在youtube api访问中使用。

我试过这个

    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()给出错误

 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."
}

有时GoogleAuthToken会抛出异常,我会收到并调用下面的

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);
            }

它会征得许可。在它结束之后,我将再次获得访问令牌,我将调用第一行代码。但同样的错误正在重复。

提前致谢

1 个答案:

答案 0 :(得分:1)

是的,我在Android中解决了这个问题

这是由于给应用程序的API访问权限。 对于每个开发人员机器,SHA1应该被赋予API并为每个SHA1创建单独的Android API密钥。

  1. 转到console.developers.google.com - >选择你的项目
  2. API& AUTH - >凭证
  3. 创建新的客户端密钥(尽管它有其他客户端密钥)
  4. 选择已安装的应用 - >机器人
  5. 正确提供您的包名称和SHA1 选择确定
  6. 您的问题将自动解决。