Google play services sdk: Requesting for offline access during auth

廉价感情. 提交于 2019-12-05 02:56:51

问题


Is there a way to request for offline access using the Google play services sdk on android? I know that the raw HTTP api has an option to do this by requesting for a refresh token, but couldn't find a way to do it via the new Google Play services sdk.

The new sdk gives the app an access token using the GoogleAuthUtil.getToken() method, but the access token expires every hour. I could make the raw http request and have the user sign in from a web view or the browser, but would prefer a way to do it natively using the sdk, since that is a much better experience for the user.


回答1:


Searching the Google docs near and far, it does look like that this is possible. Google calls this "Cross-client Identity" under its "Hybrid Apps" category.

You can apparently massage the string for the scope parameter you pass into GoogleAuthUtil.getToken(...) method to coax it into returning an "Authorization Code" rather than an OAuth2 Token. (For the difference between these, I've found this chart helpful.)

The details are here, specifically the last section titled "Android app obtains offline access for Web back-end".

It seems you'll need to pass in the following as "scope" string to getToken:

oauth2:server:client_id:<your_server_client_id>:api_scope:<scope_url_1> <scope_url_2> ...

The doc then claims this:

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.

Disclaimer: I've not tried this myself yet; our Android developer will shortly. Please report if this is working for you.



来源:https://stackoverflow.com/questions/15513909/google-play-services-sdk-requesting-for-offline-access-during-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!