【问题标题】:Stocktwits API - AuthorizationCodeGrant HttpResponseException: 400 Bad RequestStocktwits API - AuthorizationCodeGrant HttpResponseException: 400 Bad Request
【发布时间】:2012-09-28 13:17:32
【问题描述】:

在 oauth2 舞蹈的第 1 步之后,我可以在重定向 URL 中检索代码。这工作正常。但是,

我遇到了“com.google.api.client.http.HttpResponseException: 400 Bad Request”错误

试图获取 accessTokenResponse。知道为什么吗?

AuthorizationCodeGrant request = new AuthorizationCodeGrant(new NetHttpTransport(),
                                new JacksonFactory(),
                                OAuth2ClientCredentials.ACCESS_TOKEN_URL,
                                OAuth2ClientCredentials.CLIENT_ID, 
                                OAuth2ClientCredentials.CLIENT_SECRET,
                                code,
                                OAuth2ClientCredentials.REDIRECT_URI);

                        try {
                            AccessTokenResponse accessTokenResponse = request.execute();
                            CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
                            credentialStore.write(accessTokenResponse );
                        } catch (IOException e) {
                            Log.e(TAG, "error= "+e);
                            e.printStackTrace();
                        }

这是触发错误的行:

AccessTokenResponse accessTokenResponse = request.execute();

我正在使用“com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.AuthorizationCodeGrant”

我应该使用其他东西吗?有什么建议吗?

【问题讨论】:

标签: android oauth-2.0 stocktwits


【解决方案1】:

请参阅authorization flowoauth/token end-point 的第 5 步。

似乎您缺少grant_type。

【讨论】:

  • 那时我不能使用 AuthorizationCodeGrant(因为它不需要 TOKEN_GRANT_TYPE)。你会建议改用什么?我想知道您是否对外部库有任何偏好?
  • 您可以尝试手动或通过命令行上的 curl 来获取 access_token 甚至在客户端进行,如果一切都失败了。
【解决方案2】:

这看起来有效:

// Create a new HttpClient and Post Header
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(OAuth2ClientCredentials.ACCESS_TOKEN_URL);

                        try {
                            // Add your data
                            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                            nameValuePairs.add(new BasicNameValuePair("client_id", OAuth2ClientCredentials.CLIENT_ID));
                            nameValuePairs.add(new BasicNameValuePair("client_secret", OAuth2ClientCredentials.CLIENT_SECRET));
                            nameValuePairs.add(new BasicNameValuePair("code", code));
                            nameValuePairs.add(new BasicNameValuePair("grant_type", OAuth2ClientCredentials.TOKEN_GRANT_TYPE));
                            nameValuePairs.add(new BasicNameValuePair("redirect_uri", OAuth2ClientCredentials.REDIRECT_URI));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                            Log.i(TAG, "httppost= "+inputStreamToString(httppost.getEntity().getContent()));

                            // Execute HTTP Post Request
                            HttpResponse response = httpclient.execute(httppost);
                            Log.i(TAG, "response= "+inputStreamToString(response.getEntity().getContent()));

                        } catch (ClientProtocolException e) {
                            Log.e(TAG, "error= "+e);
                        } catch (IOException e) {
                            Log.e(TAG, "error= "+e);
                        }

重定向 url 包含我需要的访问令牌。

【讨论】:

  • 很高兴你明白了,一旦你在那里有了 grant_type 就可以工作了。
猜你喜欢
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 2014-07-07
相关资源
最近更新 更多