2

我一直在尝试解决这个问题一段时间,似乎无法接受我尝试过的任何事情。

我似乎无法检索刷新令牌,它会显示为空白。

 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,过期时间 = 3599,刷新令牌 = ,范围 =

知道刷新令牌=“”在哪里吗?我添加了“&access_type=offline”,但仍然没有运气。任何帮助都深表感谢!

4

1 回答 1

3

我只是想出了如何获取刷新令牌,我会告诉你我做了什么,以防万一有人遇到同样的问题......

        String authorizationUrl = new GoogleAuthorizationRequestUrl(        
            OAuth2ClientCredentials.CLIENT_ID,
            OAuth2ClientCredentials.REDIRECT_URI,
            OAuth2ClientCredentials.SCOPE).build();

这段代码首先被调用来启动整个授权过程。其中缺少的是 SO 上的几个人所建议的,您需要在 URL 链接中包含“ access_type=offline&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
于 2013-05-16T16:50:22.193 回答