如何在Android中配置OAuth2连接?

时间:2017-09-05 07:33:49

标签: android rest post oauth-2.0 okhttp

我需要通过 OAuth2 服务器将我的应用连接到安全的后端。主要问题是我无法获得令牌。我有必要的参数,如clientID,secret和继续。但我需要发出 POST 请求。我决定使用 OkHttp OAuth2客户端library' GitHub)。它不起作用,我不确定它是否发布。所以我写了一个简单的OkHttp请求并将所有内容放到post方法中。但我仍然得到int 0x10。 它看起来像这样:

token=null

然后我希望将令牌视为响应的一部分。其他版本就像是图书馆'github的指南。也许有人与此合作或者可以告诉更好的解决方案?

1 个答案:

答案 0 :(得分:2)

我不得不在之前的项目中集成OAuth 2.0以使用Google联系人。我使用了这个lib:https://github.com/openid/AppAuth-Android

您还可以在此处查看一些重要文档: https://developers.google.com/identity/protocols/OAuth2

一个例子:

<强> 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) {
               ...
              }
            });
    }
}