gpt4 book ai didi

android - 获取谷歌一次性授权码

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:49 24 4
gpt4 key购买 nike

我无法从 Google 获取一次性授权码。我正在尝试从 Android 客户端获取授权代码,以便将其发送到我的 Rails 后端(Web 客户端)。

在我的 Google Cloud Developer Console 中,我有一个具有两个客户端 ID 的应用程序:

  1. Web 应用程序的客户端 ID(用于我的 Rails 后端)

  2. Android 应用程序的客户端 ID(对于我的 android 客户端)。使用的 SHA1 来自 ~/.android/debug.keystore

假设 Web 应用程序客户端 ID 是 12345.apps.googleusercontent.com

假设 Android 客户端 ID 是 67890.apps.googleusercontent.com

这是我的一些代码:

private final static String WEB_CLIENT_ID = "12345.apps.googleusercontent.com";
private final static String GOOGLE_CALENDAR_API_SCOPE = "audience:server:client_id:" + WEB_CLIENT_ID;

private void getAndUseAuthToken(final String email) {
AsyncTask task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... emails) {
try {
return GoogleAuthUtil.getToken(AddExternalCalendarsActivity.this, emails[0], GOOGLE_CALENDAR_API_SCOPE);
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), IntentConstants.REQUEST_GOOGLE_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(String authToken) {
if (authToken != null) {
saveTokenAndGetCalendars(email, authToken);
}
}
};

String[] emails = new String[1];
emails[0] = email;
task.execute(emails);
}

一些补充说明

  • 我遇到了 GoogleAuthException 并收到“Unknown”作为消息详细信息

  • 我无法在此项目的 Google Cloud Console 权限中添加其他成员 - 添加新成员时会出现一个弹出窗口,其中显示“服务器错误。糟糕!我们的问题。”。我已向 Google 发送过两次反馈。

  • 我指的是这个 documentation .请注意下面的引用。通过“固定”,他们是说我不需要在我的 GOOGLE_CALENDAR_API_SCOPE 变量中添加 audience:server:client_id 吗?我已经尝试过有和没有,但仍然得到相同的 GoogleAuthException。

In this situation, the Android app can call the GoogleAuthUtil.getToken() method on behalf of any of the Google accounts on the device, and with a scope argument value of audience:server:client_id:9414861317621.apps.googleusercontent.com. The prefix audience:server:client_id: is fixed, and the rest of the scope string is the client ID of the web component.

  • 如果我使用此范围,我可以从设备向 google 进行身份验证。但是,我读过的文档表明我需要在范围内使用服务器 Web 客户端 ID(与 Android 客户端 ID 在同一个 Google 控制台项目中),以便服务器代表在android客户端授权的用户:

    private final static String GOOGLE_CALENDAR_API_SCOPE = "oauth2:https://www.googleapis.com/auth/calendar";

更新 1

我最初在回答中添加:我的第一个问题的原因 - 我遇到了 GoogleAuthException 并收到“未知”作为消息详细信息 - 是我在云控制台中添加 android 客户端 ID 时犯的一个错误。 SHA1 是正确的,但我没有正确输入包名称。当我的 android 包实际上是 com.company.android.app 时,我使用了 com.company.app。原始问题中的代码工作正常。只需确保您的 Google Cloud Console 项目中拥有所有必要的客户端即可。

但是另一个问题仍然存在。当我将从 GoogleAuthUtil.getToken() 返回的一次性授权 token 发送到 Rails 后端,然后尝试将其交换为 access_token 和 refresh_token 时,我得到以下信息:

Signet::AuthorizationError:
Authorization failed. Server message:
{
"error" : "invalid_grant"
}

google documentation一些 SO 帖子建议我需要设置 access_type=offline。但我认为那是当您从 Web 服务器请求一次性授权代码和离线访问时。我正在尝试从 Android 客户端请求一次性授权代码并将其发送到 Web 服务器。

GoogleAuthUtil.getToken() 是否可行?

更新 2

即使您只是尝试访问日历,Google Plus 登录也必须在范围内:

private final static String GOOGLE_CALENDAR_API_SCOPE = "oauth2:server:client_id:" + WEB_CLIENT_ID + ":api_scope:https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/calendar";

SO post很有帮助。另外,Google's Cross Client identity documentation状态:

[Note: This policy in being rolled out gradually. For the moment, when access tokens are involved, it only applies when the requested scopes include https://www.googleapis.com/auth/plus.login.]

如果 token 交换在 Rails 后端上运行,我将在答案中总结。

最佳答案

有两件事帮我解决了这个问题:

  1. 确保在同一 Google Cloud Console 项目中正确设置了 Android 和 Web 客户端 ID。

  2. 使用正确的范围。即使您只访问日历 API,也需要额外登录:

    //为访问和刷新 token 交换授权码的 Web 服务器的 ID

    private final static String WEB_CLIENT_ID = "12345.apps.googleusercontent.com";private final static String GOOGLE_CALENDAR_API_SCOPE = "oauth2:server:client_id:"+ WEB_CLIENT_ID + ":api_scope: https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/calendar ";

关于android - 获取谷歌一次性授权码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21389267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com