gpt4 book ai didi

java - 不记名代码返回 Twitter 搜索 API V1.1 的 HTTP 400 错误

转载 作者:行者123 更新时间:2023-12-04 04:52:51 26 4
gpt4 key购买 nike

我正在尝试实现 Twitter Search API V1.1

如果我错了,请纠正我。
我执行了以下提到的步骤:

Step 1) Created an App in Twitter.
So I got the TWITTER_CONSUMER_KEY and TWITTER_CONSUMER_SECRETCODE.

Step 2) I encoded the concatenation of the above keys separated by ":" with the base UTF-8.

Step3 ) Get the bearer token with the above generated code.

Step4 ) Use the bearer code to get the Tweets on the relevance of a keyword.

我被困在第 3 步,

我在哪里得到响应为:
Server returned HTTP response code: 400 for URL: https://api.twitter.com/oauth2/token
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at com.tcs.crm.socialCRM.action.TwitterIntegration.requestBearerToken(TwitterIntegration.java:74)
at com.tcs.crm.socialCRM.action.TwitterIntegration.getStatusSearch(TwitterIntegration.java:27)
at com.tcs.crm.socialCRM.action.TwitterIntegration.main(TwitterIntegration.java:103)

我的代码是::
HttpsURLConnection connection = null;
PrintWriter outWriter = null;
BufferedReader serverResponse = null;

try
{
URL url = new URL(endPointUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Host", "api.twitter.com");
connection.setRequestProperty("User-Agent", "Search Tweets");
connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Content-Length", "29");
connection.setUseCaches(false);
connection.setDoOutput( true );


logger.info("Point 1");

//CREATE A WRITER FOR OUTPUT
outWriter = new PrintWriter( connection.getOutputStream() );

logger.info("Point 2");

//SEND PARAMETERS
outWriter.println( "grant_type=client_credentials" );
outWriter.flush();
outWriter.close();

logger.info("Point 3");
//RESPONSE STREAM
serverResponse = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );

JSONObject obj = (JSONObject)JSONValue.parse(serverResponse);


logger.info("The return string is "+obj.toString());
return obj.toString();

请让我知道如何解决此问题。

最佳答案

我对 Twitter 的不记名 token 有同样的问题。此外,我测试了您的相同代码,并收到了错误 403。之后,我创建了自定义方法以从 twitter 获取不记名 token ,并得到了解决方案。

    HttpClient httpclient = new DefaultHttpClient();

String consumer_key="YOUR_CONSUMER_KEY";
String consumer_secret="YOUR_CONSUMER_SECRET";

// Following the format of the RFC 1738
consumer_key=URLEncoder.encode(consumer_key, "UTF-8");
consumer_secret=URLEncoder.encode(consumer_secret,"UTF-8");

String authorization_header_string=consumer_key+":"+consumer_secret;
byte[] encoded = Base64.encodeBase64(authorization_header_string.getBytes());


String encodedString = new String(encoded); //converting byte to string

HttpPost httppost = new HttpPost("https://api.twitter.com/oauth2/token");
httppost.setHeader("Authorization","Basic " + encodedString);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();

parameters.add(new BasicNameValuePair("grant_type", "client_credentials"));

httppost.setEntity(new UrlEncodedFormEntity(parameters));
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);

祝你好运!

关于java - 不记名代码返回 Twitter 搜索 API V1.1 的 HTTP 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17170187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com