- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我无法从 Google 获取一次性授权码。我正在尝试从 Android 客户端获取授权代码,以便将其发送到我的 Rails 后端(Web 客户端)。
在我的 Google Cloud Developer Console 中,我有一个具有两个客户端 ID 的应用程序:
Web 应用程序的客户端 ID(用于我的 Rails 后端)
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 后端上运行,我将在答案中总结。
最佳答案
有两件事帮我解决了这个问题:
确保在同一 Google Cloud Console 项目中正确设置了 Android 和 Web 客户端 ID。
使用正确的范围。即使您只访问日历 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/
这两个包看起来非常相似: http://www.passportjs.org/packages/passport-google-oauth2/ http://www.passportjs.org/pa
我想在我的网站上添加通过 Google 和 Twitter 登录的按钮。我需要只使用应用程序的客户端而不是服务器端来完成此操作。但我没有找到任何 API。对于我发现的所有内容,我需要使用带有 key
我使用此链接通过 google plus 共享我的页面。 https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fcompany%2
我正在尝试学习 google API,并且我的经验是使用 Python,因此我尝试使用 google api python 客户端来访问一些 google 服务,但在构建服务对象时遇到错误。 从 ap
在其实际的实时托管平台上构建实时站点的努力中,有没有办法告诉谷歌不要索引该网站?我发现了以下内容: http://support.google.com/webmasters/bin/answer.py
我正在开发一个 iOS 应用程序。当我运行用于 google+ 登录的程序时,在我点击允许访问按钮后,会显示此消息。 You've reached this page because we have
我有一个非常复杂的网站,每个页面包含 11 个 js 文件。 我最近添加了 google +1 按钮,代码如下: 这会正确显示 +1 按钮,直到我单击它。当我单击它时,出现此错误:https://
我正在尝试使用 google API 创建一个 html 文件,以便在 google MAPS 上显示 KML 文件。 这是 HTML 代码: function initMap() {
我是使用 Google Benchmark 的新手,在本地运行代码与在 Quick-Bench.com 上运行代码时,我收到了运行相同基准测试(下方)的不同结果,该基准测试使用 C++ 检索本地时间.
我已按照 Google 网站上的说明通过添加以下元标记在我的 AngularJS 网站上启用 Ajax 抓取: 呈现的内容有一些链接,如: User 1 User 2 User 3 还有一些呈现动态
通过 Google 手册实现 Google AppInvite - link . 启动 Invite Activity 并在 LogCat 中获取下一步: E/AppInviteAgent: Get
那么有人用过 Google 的 Go 吗?我想知道数学性能(例如触发器)与其他具有垃圾收集器的语言(如 Java 或 .NET)相比如何? 有人调查过吗? 最佳答案 理论性能:纯 Go 程序的理论性能
Stackdriver 测试我的网站启动速度慢 我们使用 cloudflare 作为我们的站点 CDN 提供商。我们使用 stackdriver 从外部测试站点可用性,我们将时间检查间隔设置为 1 分
我正在尝试使用 stax.GeneralConv() ( https://jax.readthedocs.io/en/latest/_modules/jax/experimental/stax.htm
我有一个从谷歌金融中提取日内数据的软件。但是,由于昨天 Google 更新了 API,所以软件报错了 Conversion from string HTML HEAD meta http-equiv=
我们在尝试从 Google 获取 oAuth token 时遇到“redirect_uri_mismatch”错误: [client 127.0.0.1:49892] {\n "error" : "
我的网站正在使用 Google reCAPTCHA 控件,但我听说它被阻止了 中国,反正我看到有人报告说将 API 更改为 https://www.recaptcha.net在中国工作? Anyone
背景 WordPress Google Adsense 谷歌自动插入 anchor 定广告 https://pptmon.com 问题 如下图所示,主播广告的容器高度太大了! 如何调整高度? 这是谷歌
我在使用 Google Colab 时遇到问题。当我想制作一个新的 Python3 Notebook 时,由于我登录了我的 Google 帐户,因此无法加载刚刚打开的新页面。 我该怎么办? 感谢您的帮
我正在使用 facebook和 google oauth2使用 passport js 登录, 有了这个流 用户点击登录按钮 重定向到 facebook/google auth 页面(取决于用户选择的
我是一名优秀的程序员,十分优秀!