有没有办法在 Android 上使用 Google Play 服务 SDK 请求离线访问?我知道 raw HTTP api可以选择通过请求刷新 token 来执行此操作,但无法通过新的 Google Play services sdk 找到执行此操作的方法.
新的 SDK 使用 GoogleAuthUtil.getToken()
方法为应用程序提供访问 token ,但访问 token 每小时都会过期。我可以发出原始 http 请求并让用户从 WebView 或浏览器登录,但我更喜欢使用 sdk 在 native 执行此操作的方法,因为这对用户来说是更好的体验。
最佳答案
四处搜索谷歌文档,看起来这确实是可能的。 Google 在其 "Hybrid Apps" 下称此为“跨客户端身份”类别。
您显然可以修改传递给 GoogleAuthUtil.getToken(...)
方法的范围参数的字符串,以诱导它返回“授权代码”而不是 OAuth2 token 。 (对于它们之间的区别,我发现 this chart 很有帮助。)
详情为here ,特别是标题为 "Android app obtains offline access for Web back-end" 的最后一节.
看来您需要将以下内容作为“范围”字符串传递给 getToken:
oauth2:server:client_id:<your_server_client_id>:api_scope:<scope_url_1> <scope_url_2> ...
然后文档声称:
In this case, GoogleAuthUtil.getToken() will first require that the user has authorized this Project for access to the two scopes. Assuming this is OK, it will return, not an OAuth token, but a short-lived authorization code, which can be exchanged for an access token and a refresh token.
免责声明:我自己还没有尝试过;我们的 Android 开发人员很快就会。如果这对您有用,请报告。
关于android - 谷歌播放服务 sdk : Requesting for offline access during auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513909/