Reputation: 2019
I need to connect my app to a secure back-end via OAuth2 server. The main problem is that I can't get a token. I have the necessary parameters like clientID, secret and go on. But I need to make a POST request. I decided to use OkHttp OAuth2 client (library' GitHub). It doesn't work and I'm not sure it makes a POST. So I wrote simple a OkHttp request and put all stuff to post method. But I still get token=null
.
It looks like this:
OkHttpClient client1 = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("client_id", clientID)
.add("client_secret", clientSecret)
.add("grant_type", "password")
.build();
Request request = new Request.Builder()
.url(site)
.post(formBody)
.build();
And then I expect to see the token as a part of response. Other version is like at a guide at library' github. Maybe someone worked with this or can tell a better solution?
Upvotes: 0
Views: 2229
Reputation: 855
I had to integrate OAuth 2.0 in a project a while ago to use Google contacts. I used this lib: https://github.com/openid/AppAuth-Android
You can also see some important documentation in here: https://developers.google.com/identity/protocols/OAuth2
And an example:
Fragment.java:
AuthorizationServiceConfiguration serviceConfiguration =
new AuthorizationServiceConfiguration(
Uri.parse(GoogleConstants.OAUTH_URL) /* auth endpoint */,
Uri.parse(GoogleConstants.TOKEN_URL) /* token endpoint */,
null
);
AuthorizationRequest.Builder authRequestBuilder = new AuthorizationRequest.Builder(
serviceConfiguration,
getString(GoogleConstants.CLIENT_ID),
ResponseTypeValues.CODE,
Uri.parse(GoogleConstants.REDIRECT_URI))
.setScope(GoogleConstants.OAUTH_SCOPE);
AuthorizationRequest request = authRequestBuilder.build();
AuthorizationService authorizationService = new AuthorizationService(getActivity());
String action = GoogleConstants.APP_ACTION;
Intent postAuthorizationIntent = new Intent(getActivity(), ExampleActivity.class);
postAuthorizationIntent.setAction(action);
PendingIntent pendingIntent =
PendingIntent.getActivity(getActivity(), 0, postAuthorizationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
authorizationService.performAuthorizationRequest(request, pendingIntent);
Activity.java:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
checkIntentAction(intent);
}
...
private void checkIntentAction(@Nullable Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case GoogleConstants.APP_ACTION:
if (!intent.hasExtra(USED_INTENT)) {
handleAuthorizationResponse(intent);
intent.putExtra(USED_INTENT, true);
}
break;
default:
// do nothing
}
}
}
}
...
private void handleAuthorizationResponse(final @NonNull Intent intent) {
final AuthorizationResponse response = AuthorizationResponse.fromIntent(intent);
if (response != null) {
final AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(response.createTokenExchangeRequest(),
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(@Nullable TokenResponse tokenResponse,
@Nullable AuthorizationException exception) {
...
}
});
}
}
Upvotes: 2