I am configuring uber sdk with Request scope (Restricted). In the LoginManager callback method onAuthorizationCodeReceived() I am getting authorizationCode as a parameter, whereas the onLoginSuccess() callback method is not being called.
Here is my code...
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);
}
});
And here is the initialiseUberSDK() method...
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;
}
Here onLoginSuccess() method never being called. Only onAuthorizationCodeReceived() method is being called (with access token object being null).
My question is
how to generate access token using the authorization code?
Below is the java doc of the onAuthorizationCodeReceived() method...
*
public void onAuthorizationCodeReceived(@NonNull String
authorizationCode)
Description copied from interface: LoginCallback Called when authorization code has been returned to the redirect uri. AccessToken
must be retrieved using Client Secret, see
https://developer.uber.com/docs/authentication#section-step-two-receive-redirect
Specified by:
onAuthorizationCodeReceived in interface LoginCallback
Parameters:
authorizationCode - the authorizationCode that can be used to retrieve AccessToken
*
You posted SO question as well. Plase, use the authorization code as per this sample you submitted - because it is confirmed from our logs that you managed to get a valid access token as a response (KA.eyJ2ZXJzaW9uIjoyLCJ*****). Also, you successfully initiate new ride request. So code you used to achieve it is a valid code.
If this does not answer your question please check Android SDK sample.
Yes, you are right that I manage to get the access token using the authorization code. I have gone through your REST web service doc and found out that using a POST request to server with a valid authorization code, I can get the required parameters to generate an access token.
Here is my updated code for getting auth code in the callback and request server using HTTP POST method to generate access token...
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);
The problem with the API is there is no mention of requesting server to POST method for getting the access token. Also there is no mention of the onAuthorizationCodeReceived() callback method in the doc.
And also, there should be a JAVA interface for doing this operation as everything else is doing that (OR at least in the doc they should mention the process).
Whatever, I have found out the solution yesterday evening on my own (14 hours ego) and now (today morning) I am posting the solution. Hope, this will help someone.
NOTE: I have used ION library as a network client.