代码之家  ›  专栏  ›  技术社区  ›  VolodymyrH

如何在Android中配置OAuth2连接?

  •  0
  • VolodymyrH  · 技术社区  · 7 年前

    OAuth2 服务器主要问题是我拿不到代币。我有一些必要的参数,比如clientID、secret等等。但我需要做一个 岗位 OkHttp OAuth2客户端 ( library' GitHub ). 它不起作用,我也不确定它是否能发帖子。所以我编写了一个简单的OkHttp请求,并将所有内容放到post方法中。但我还是 token=null .

    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();
    

    然后我希望看到令牌作为响应的一部分。另一个版本类似于图书馆github的指南。也许有人能解决这个问题,或者能找到更好的解决方案?

    1 回复  |  直到 7 年前
        1
  •  2
  •   luiscosta    7 年前

    不久前,为了使用谷歌联系人,我不得不将OAuth 2.0集成到一个项目中。我使用了这个库: https://github.com/openid/AppAuth-Android

    您还可以在这里看到一些重要的文档: https://developers.google.com/identity/protocols/OAuth2

    碎片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);
    

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