【问题标题】:android: google APi OAuth 2.0 - error 400 getting user infoandroid: google APi OAuth 2.0 - 获取用户信息时出现错误 400
【发布时间】:2012-06-26 18:16:24
【问题描述】:

我在 web servlet 上有以下内容:

已编辑:

public String tryGoogleAuthentication(String auth_token){

    HttpURLConnection connection = null;
    try {

        //connection = (HttpURLConnection) new URL(("https://www.googleapis.com/oauth2v1/tokeninfo?access_token={"+auth_token+"}")).openConnection();
       connection = (HttpURLConnection) new URL(("https://www.googleapis.com/oauth2/v1/user info")).openConnection();
       connection.setRequestMethod("GET");          
       connection.setRequestProperty("Authorization", "Bearer {"+auth_token+"}");
       connection.setRequestProperty("Host", "googleapis.com");

        //read response
        String response = fromInputStreamToString(connection.getInputStream());
        System.out.println(response);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return CONST.STATUS_OK;
}

在安卓中:

    private void googleAuthenticate(){
    try {
        mOAauthHelper = new OAuthHelper("something.net", "xxxxxxxxxx", 
                "https://www.googleapis.com/auth/userinfo.profile", "alex://myScheme");
        String uri = mOAauthHelper.getRequestToken();

        startActivity(new Intent("android.intent.action.VIEW", Uri.parse(uri)));

                    //Intent i = new Intent(this, GoogleOAUTHActivity.class);
                    //i.putExtra(GoogleOAUTHActivity.GOOGLE_OAUTH_ENDPOINT_KEY, uri);           
                     //startActivity(i);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthMessageSignerException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthNotAuthorizedException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthExpectationFailedException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthCommunicationException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    }
}

@Override
protected void onNewIntent(Intent intent) {
    //super.onNewIntent(intent);

    Uri uri = intent.getData();
    String oauthToken = uri.getQueryParameter("oauth_token");
    String oauthVerifier = uri.getQueryParameter("oauth_verifier");

    if(oauthToken != null){
        authorizeGoogleSessionToServer(oauthToken);
    }
}

在此之后,我将请求令牌发送到我的 servlet,我试图在其中获取用户配置文件,但没有成功。

您能否告诉我出了什么问题以及为什么我从 google 收到错误 400?

谢谢。

【问题讨论】:

    标签: java android oauth-2.0 gdata-api


    【解决方案1】:

    不幸的是,我已经看到了一些问题

    1. 你在你的 URL 中的should never havecurly braces,甚至在草案中所述的Bearer header

    connection = (HttpURLConnection) new URL(("https://www.googleapis.com/oauth2/v1/userinfo?access_token={"+auth_token+"}")).openConnection()

    1. 400 表示您在请求中遗漏了某些内容,在与特定错误节点相同的响应中可能有更多关于它的信息。

    2. 最后,请注意,oauth_verifier 参数来自 OAuth 1。

    我建议您使用Google OAuth2 playground 测试您的请求网址

    祝你好运!

    【讨论】:

    • 3.我不使用 oauthVerifier。
    • 1.请参阅:developers.google.com/accounts/docs/OAuth2Login#userinfocall。我更改了请求(删除大括号并添加了标题)。请参阅上面的编辑。
    • 另外,使用 Google OAuth2 Playground 并点击查找可用 URI,我得到:“发生了一些不好的事情:invalid_token”
    • 如果可能的话,你能给我发一个请求示例吗?花括号出现在谷歌文档中:developers.google.com/accounts/docs/OAuth2Login#userinfocall
    • 我也尝试获取 www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token,但返回 401 错误。
    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 2021-11-05
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多