2017-10-16 88 views
0

我正在配置具有請求範圍(受限)的uber sdk。在LoginManager回調方法onAuthorizationCodeReceived()我得到authorizationCode作爲參數,而onLoginSuccess()回調方法沒有被調用。如何在登錄回撥中使用授權碼獲取UBER訪問令牌

這裏是我的代碼...

config = initialiseUberSDK(); 
accessTokenManager = new AccessTokenManager(this); 
loginManager = new LoginManager(accessTokenManager, 
    new LoginCallback() { 

     @Override 
     public void onLoginCancel() { 
      Toast.makeText(CustomActivity2.this, "Login cancelled", Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onLoginError(@NonNull AuthenticationError error) { 
      Toast.makeText(CustomActivity2.this, 
           "Error: "+error.name(), Toast.LENGTH_LONG) 
           .show(); 
     } 

     @Override 
     public void onLoginSuccess(@NonNull AccessToken accessToken) { 
      Toast.makeText(CustomActivity2.this, "Login success", 
           Toast.LENGTH_LONG) 
           .show(); 
         createSession(); 
     } 

     @Override 
     public void onAuthorizationCodeReceived(@NonNull String authorizationCode) { 
      Toast.makeText(CustomActivity2.this, "Your Auth code is: "+authorizationCode, 
           Toast.LENGTH_LONG) 
           .show(); 
      } 
     }, 
     config, 
     1113).setRedirectForAuthorizationCode(true); 
    customButton = (Button) findViewById(R.id.button); 
    customButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     loginManager.login(CustomActivity2.this); 
    } 
}); 

這裏是initialiseUberSDK()方法...

private SessionConfiguration initialiseUberSDK() { 
     config = new SessionConfiguration.Builder() 
       .setClientId(getResources().getString(R.string.client_id)) 
//    .setServerToken(getResources().getString(R.string.server_token)) 
//    .setClientSecret(getResources().getString(R.string.client_secret)) 
       .setRedirectUri(getResources().getString(R.string.redirect_url)) 
       .setEnvironment(SessionConfiguration.Environment.SANDBOX) 
       .setScopes(Arrays.asList(Scope.PROFILE, Scope.RIDE_WIDGETS, Scope.REQUEST)) 
       .build(); 
//  UberSdk.initialize(config); 
     return config; 
    } 

這裏onLoginSuccess()方法永遠不會被調用。只有onAuthorizationCodeReceived()方法被調用(訪問令牌對象爲空)。

我的問題是

如何使用授權碼生成訪問令牌?

下面是onAuthorizationCodeReceived()方法的Java文檔...

*

公共無效onAuthorizationCodeReceived(@NonNull字符串 authorizationCode)

從接口複製: LoginCallback授權碼返回到重定向uri時調用。的accessToken 必須使用客戶端密鑰被檢索,見 https://developer.uber.com/docs/authentication#section-step-two-receive-redirect 指定者: onAuthorizationCodeReceived接口LoginCallback 參數: authorizationCode - 可用於檢索的accessToken的authorizationCode

*

回答

0

你SO question也是如此。按照您提交的這個示例使用授權代碼 - 因爲從我們的日誌中確認您設法獲得有效的訪問令牌作爲響應(KA.eyJ2ZXJzaW9uIjoyLCJ *****)。此外,您還成功啓動了新的駕駛要求。所以你用來實現它的代碼是一個有效的代碼。
如果這不能回答你的問題,請檢查Android SDK sample

0

是的,你是對的,我設法使用授權碼獲取訪問令牌。我已經瀏覽了您的REST Web服務文檔,發現通過使用有效的授權代碼向服務器發送POST請求,我可以獲取所需的參數以生成訪問令牌。

這是我更新的使用HTTP POST方法來生成訪問令牌獲取授權碼回調,並請求服務器代碼...

loginManager = new LoginManager(accessTokenManager, 
       new LoginCallback() { 

        @Override 
        public void onLoginCancel() { 
         Toast.makeText(CustomActivity2.this, "Login cancelled", Toast.LENGTH_LONG).show(); 
        } 

        @Override 
        public void onLoginError(@NonNull AuthenticationError error) { 
         Toast.makeText(CustomActivity2.this, 
           "Error: "+error.name(), Toast.LENGTH_LONG) 
           .show(); 
        } 

        @Override 
        public void onLoginSuccess(@NonNull AccessToken accessToken) { 
         Toast.makeText(CustomActivity2.this, "Login success", 
           Toast.LENGTH_LONG) 
           .show(); 
        } 

        @Override 
        public void onAuthorizationCodeReceived(@NonNull String authorizationCode) { 
         Toast.makeText(CustomActivity2.this, "Your Auth code is: "+authorizationCode, 
           Toast.LENGTH_LONG) 
           .show(); 
         Ion.with(getApplicationContext()) 
           .load("POST", "https://login.uber.com/oauth/v2/token") 
           .setBodyParameter("client_id",getResources().getString(R.string.client_id)) 
           .setBodyParameter("client_secret",getResources().getString(R.string.client_secret)) 
           .setBodyParameter("grant_type","authorization_code") 
           .setBodyParameter("redirect_uri",getResources().getString(R.string.redirect_url)) 
           .setBodyParameter("code",authorizationCode) 
           .setBodyParameter("scope","profile history request") 
           .asString() 
           .setCallback(new FutureCallback<String>() { 
            @Override 
            public void onCompleted(Exception e, String result) { 
             try { 
              JSONObject jsonObject = new JSONObject(result); 
              if (result.contains("error")) { 
               String error = jsonObject.getString("error"); 
               Toast.makeText(CustomActivity2.this, error, Toast.LENGTH_SHORT).show(); 
              }else { 
               String accessTokenTemp = jsonObject.getString("access_token"); 
               String expiresInTemp = jsonObject.getString("expires_in"); 
               String lastAuthenticatedTemp = jsonObject.getString("last_authenticated"); 
               String refreshTokenTemp = jsonObject.getString("refresh_token"); 
               String scopeTemp = jsonObject.getString("scope"); 
               String tokenTypeTemp = jsonObject.getString("token_type"); 

               AccessTokenManager accessTokenManager = new AccessTokenManager(getApplicationContext()); 
               int expirationTime = Integer.parseInt(expiresInTemp); 
               List<Scope> scopes = Arrays.asList(Scope.PROFILE, Scope.HISTORY, Scope.REQUEST); 
               String token = accessTokenTemp; 
               String refreshToken = refreshTokenTemp; 
               String tokenType = tokenTypeTemp; 
               AccessToken accessToken = new AccessToken(expirationTime, scopes, token, refreshToken, tokenType); 
               accessTokenManager.setAccessToken(accessToken); 

               if (accessTokenManager.getAccessToken() != null){ 
                createSession(); 
               } 
              } 
             } catch (JSONException e1) { 
              e1.printStackTrace(); 
             } 
            } 
           }); 
        } 
       }, 
       config, 
       1113).setRedirectForAuthorizationCode(true); 

與API的問題是有要求的服務器不提以POST方法獲取訪問令牌。也沒有提到doc中的onAuthorizationCodeReceived()回調方法。

而且,應該有一個JAVA接口來執行此操作,因爲其他所有操作都是這樣做的(至少在文檔中他們應該提到該過程)。無論如何,昨天晚上我自己(14小時自我)和現在(今天早上)發現瞭解決方案。希望,這將有助於某人。

注意:我已經使用ION庫作爲網絡客戶端。

相關問題