我正在使用请求范围(受限制)配置uber sdk。在LoginManager回调方法onAuthorizationCodeReceived()中,我将authorizationCode作为参数,而onLoginSuccess()回调方法未被调用。
这是我的代码......
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()方法(访问令牌对象为null)。
我的问题是
如何使用授权码生成访问令牌?
以下是onAuthorizationCodeReceived()方法的java文档...
*
public void onAuthorizationCodeReceived(@NonNull String authorizationCode)
从界面复制的说明: LoginCallback在将授权码返回到重定向uri时调用。的accessToken 必须使用Client Secret检索,请参阅 https://developer.uber.com/docs/authentication#section-step-two-receive-redirect 具体说明: onAuthorizationCodeReceived在接口LoginCallback中 参数: authorizationCode - 可用于检索AccessToken
的authorizationCode
*
答案 0 :(得分:0)
您也发布了SO question。 Plase,根据您提交的样本使用授权代码 - 因为从我们的日志中确认您设法获得有效的访问令牌作为响应(KA.eyJ2ZXJzaW9uIjoyLCJ *****)。此外,您成功启动了新的乘车请求。因此,您用来实现它的代码是有效的代码 如果这不能解答您的问题,请查看Android SDK sample。
答案 1 :(得分: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方法来获取访问令牌。此外,文档中没有提及onAuthorizationCodeReceived()回调方法。
而且,应该有一个JAVA接口来执行此操作,因为其他所有操作都是这样(或者至少在文档中他们应该提到过程)。
无论如何,我昨天晚上独自找到了解决方案(14小时自我),现在(今天早上)我发布了解决方案。希望,这会有所帮助。
注意:我已将ION库用作网络客户端。