简体   繁体   English

OAuth令牌invalid_grant

[英]OAuth token invalid_grant

I am trying to integrate the Google Plus API into my web application. 我正在尝试将Google Plus API集成到我的Web应用程序中。

I am able to authenticate with Google and I get the code. 我可以通过Google进行身份验证,并且可以获得代码。 The problem occurs when I try getting an access_token using HttpClient . 当我尝试使用HttpClient获取access_token时出现问题。

Here is some code: 这是一些代码:

DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "https://accounts.google.com/o/oauth2/token");
        post.addHeader("accept", "application/json");
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("code", myCode));
        nvps.add(new BasicNameValuePair("client_id",
                "my_client_id"));
        nvps.add(new BasicNameValuePair("redirect_uri",
                "http://localhost:8080/test/gplus.do"));
        nvps.add(new BasicNameValuePair("client_secret",
                "my_client_secret"));
        nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
        post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        HttpResponse response = httpClient.execute(post);

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (response.getEntity().getContent())));

        String output;
        while ((output = br.readLine()) != null) {
            res = res + output;
        }

I have managed to get the token in my mock application several times before, but now it seems to be failing for some unknown reason. 以前,我已经设法在我的模拟应用程序中获得了令牌,但是现在由于某种未知的原因,它似乎失败了。 All I get is error:invalid_grant . 我得到的只是error:invalid_grant Why is this happening? 为什么会这样呢?

are you sure u got the token this way before? 您确定以前以这种方式获得令牌吗? AFAIK, in oAuth, you first call /authorize to get the authorization code, and then, using this code, you make the call to /token to get the access token. AFAIK,在oAuth中,您首先调用/ authorize以获取授权代码,然后使用该代码对/ token进行调用以获取访问令牌。

ps you can use packages as Spring for oAuth , so it makes all the "behind-the-scenes" stuff, all you have to do is to configure the XML. ps,您可以将包用作Spring的oAuth ,这样它就可以完成所有“幕后”工作,所有您需要做的就是配置XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google OAuth返回invalid_grant令牌类型不正确 - Google OAuth returning invalid_grant Incorrect token type 谷歌 API oauth invalid_grant - Google API oauth invalid_grant Spring boot Oauth2 grant_type 密码总是返回 invalid_grant Bad Credentials - Spring boot Oauth2 grant_type password always return invalid_grant Bad Credentials Spring oauth2 中的 error=&quot;invalid_grant&quot;, error_description=&quot;Bad credentials&quot; - error="invalid_grant", error_description="Bad credentials" in Spring oauth2 从刷新令牌获取访问令牌失败并出现 invalid_grant 错误,并且错误请求或令牌已过期或作为错误描述被撤销 - Getting access token from refresh token failing with invalid_grant error, and Bad Request OR Token has been expired or revoked as error description Java,驱动器APIv2,appengine 1.7.3,struts2.3.4.1,OAuth2。 TokenResponseException:400确定invalid_grant - java, drive APIv2, appengine 1.7.3, struts2.3.4.1, OAuth2. TokenResponseException: 400 OK invalid_grant Gmail API服务器访问“ invalid_grant” iOS? - Gmail API server access “invalid_grant” iOS? Google Cloud Storage 和服务帐户的 invalid_grant 错误 - invalid_grant error with Google Cloud Storage and Service Account 来自授权代码交换的 invalid_grant 响应 - invalid_grant response from exchange of authorization code 通过Google API访问电子表格invalid_grant - Access spreadsheet via Google API invalid_grant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM