我一直在尝试解决这个问题一段时间,似乎无法接受我尝试过的任何事情。
我似乎无法检索刷新令牌,它会显示为空白。
public class oaUTH extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... unused) {
try {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new NetHttpTransport();
CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
AccessTokenResponse accessTokenResponse = credentialStore.read();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessTokenResponse.accessToken,
transport,
jsonFactory,
OAuth2ClientCredentials.CLIENT_ID,
OAuth2ClientCredentials.CLIENT_SECRET,
accessTokenResponse.refreshToken+"&access_type=offline");
final Latitude latitude = new Latitude(transport, accessProtectedResource, jsonFactory);
latitude.apiKey=OAuth2ClientCredentials.API_KEY;
Log.i("AGENTPORTAL", "Access Token = " + accessTokenResponse.accessToken + ", Expires in = " + accessTokenResponse.expiresIn + ", Refresh Token = " + accessTokenResponse.refreshToken + ", Scope = " + accessTokenResponse.scope);
LatitudeCurrentlocationResourceJson currentLocation = latitude.currentLocation.get().execute();
String locationAsString = convertLocationToString(currentLocation);
Log.i("TAG", locationAsString);
//textView.setText(locationAsString);
} catch (Exception ex) {
ex.printStackTrace();
//textView.setText("Error occured : " + ex.getMessage());
startActivity(new Intent().setClass(getApplicationContext(),OAuthAccessTokenActivity.class));
}
以下是日志:访问令牌= ** AHES6ZTeqgwdxjll6Gb4Cf9I0_n5bO_OdgR2OR * * WLPCPzJ5xtO5M,Expires in = 3599,Refresh Token =,Scope =
知道刷新令牌=“”的地方吗?我添加了“&amp; access_type = offline”但仍然没有运气。非常感谢任何帮助!
答案 0 :(得分:3)
我刚想出如何获得刷新令牌,我会告诉你我做了什么,万一有人遇到同样的问题......
String authorizationUrl = new GoogleAuthorizationRequestUrl(
OAuth2ClientCredentials.CLIENT_ID,
OAuth2ClientCredentials.REDIRECT_URI,
OAuth2ClientCredentials.SCOPE).build();
这段代码首先被称为启动整个授权过程。其中缺少的是SO上的几个人建议的内容,您需要在URL链接中包含“ access_type = offline&amp; approval_prompt = force ”。
为了使其有效,我只是将authorizationUrl更改为
String authorizationUrl = "https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&client_id=" + OAuth2ClientCredentials.CLIENT_ID + "&redirect_uri=" + OAuth2ClientCredentials.REDIRECT_URI + "&response_type=code&scope=" + OAuth2ClientCredentials.SCOPE