使用GoogleAuth.grantOfflineAccess在服务器上进行身份验证时出现Redirect_URI错误

时间:2017-06-05 12:48:25

标签: authentication oauth-2.0

我尝试使用https://developers.google.com/identity/sign-in/web/server-side-flow中列出的授权流程。 我已按照指示创建了凭据...没有指定授权重定向URI,因为doc指示:"授权重定向URI字段不需要值。重定向URI不与JavaScript API一起使用。"

启动授权的代码是: 客户端按钮和回调:

<script>
$('#signinButton').click(function() {


    var auth2 = gapi.auth2.getAuthInstance(); 
    auth2.grantOfflineAccess().then(signInCallback);

});
function signInCallback(authResult) {

    console.log('sending to server');

    if (authResult['code']) {

        // Send the code to the server
        $.ajax({
          type: 'POST',
          url: 'CheckAuth',

          headers: {
            'X-Requested-With': 'XMLHttpRequest'
          },
          contentType: 'application/octet-stream; charset=utf-8',
          success: function(result) {
            // Handle or verify the server response.
          },
          processData: false,
          data: authResult['code']
        });
      } else {
        // There was an error.
      }
}
</script>

服务器端(CheckAuth方法从auth代码创建凭据,它通过javascript回调正确接收):

private Credential authorize() throws Exception {
    // load client secrets
    InputStream is = new FileInputStream(clientSecretsPath_);
    InputStreamReader isr = new InputStreamReader(is);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, isr);



    String redirect_URI = "";
    GoogleTokenResponse tokenResponse =
                  new GoogleAuthorizationCodeTokenRequest(
                          httpTransport, JSON_FACTORY,
                      "https://www.googleapis.com/oauth2/v4/token",
                      clientSecrets.getDetails().getClientId(),
                      clientSecrets.getDetails().getClientSecret(),
                      token_,
                      redirect_URI)  
                      .execute();

        String accessToken = tokenResponse.getAccessToken();

        // Use access token to call API
        GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
        return credential;

    }

流程正常工作,直到我的服务器尝试交换令牌响应的授权代码(GoogleAuthorizationCodeTokenRequest.execute())...... auth服务器返回:

400 Bad Request
{
  "error" : "invalid_request",
  "error_description" : "Missing parameter: redirect_uri"
}

鉴于错误,我在javascript中查看了auth实例的调试,并注意到它指示的是redirect_uri。然后,我更新了我的Google凭据,并在授权重定向URI中指定了该URI(它是访问javascript的URL,因为auth服务器正确返回到指定的javascript回调)。使用更新的凭据和在GoogleAuthorizationCodeTokenRequest(String redirect_URI =&#34; http://example.com:8080/JavascriptLocation&#34;;)的实例化中指定的URI,然后错误变为:

400 Bad Request
{ 
  "error" : "redirect_uri_mismatch",
  "error_description" : "Bad Request"
}

我已经一直跟踪到auth服务器的实际HttpRequest(www.googleapis.com/oauth2/v4/token),并且无法分辨它正在查找的是哪个redirect_uri。

有没有人知道redirect_uri在这种情况下应该是什么价值(使用grantOfflineAccess()时)?我很乐意发布更多的代码,如果这些有用的话......只是没有想要充斥页面。谢谢。

1 个答案:

答案 0 :(得分:12)

找到&#34; postmessage&#34;在发布问题之后......使用它作为服务器端的redirect_URI似乎从auth服务器生成成功的响应。所以...设置redirect_URI =&#34; postmessage&#34;在下面的代码中似乎适用于这种情况。

GoogleTokenResponse tokenResponse =
                  new GoogleAuthorizationCodeTokenRequest(
                          httpTransport, JSON_FACTORY,
                      "https://www.googleapis.com/oauth2/v4/token",
                      clientSecrets.getDetails().getClientId(),
                      clientSecrets.getDetails().getClientSecret(),
                      token_,
                      redirect_URI) 
                      .execute();
相关问题