2013-05-15 63 views
2

我一直试图弄清楚这一点现在,似乎无法得到我试过的任何东西。Android oAuth刷新标记返回null

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

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”,但仍然没有运气。任何帮助深表感谢!

+0

谁能帮助? –

回答

3

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

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

这段代码是什么首先被调用来启动整个授权过程。这里缺少的是这里几个人建议的,你需要在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