GoogleAuthToken返回没有访问权限的访问令牌,并抛出错误



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

它将请求许可。在它结束后,我将获得访问令牌,因为我再次调用第一行代码。但是同样的错误重复出现。

thanks in advance

是的,我解决了Android的问题

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

  1. 去console.developers.google.com ->选择你的项目
  2. API和;AUTH ->凭证
  3. 创建新的客户端密钥(尽管它有其他客户端密钥)
  4. 选择已安装的app -> android
  5. 正确给出你的包名和SHA1选择OK

你的问题将自动解决。

相关内容

  • 没有找到相关文章

最新更新