gpt4 book ai didi

android - 从 Android 到 twitter API 的 Oauth2 连接(仅应用程序身份验证)

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:47 26 4
gpt4 key购买 nike

我有一个 Android 应用程序可以显示来自某个特定用户的 Twitter 回复。自升级到 1.1 版 API 以来,我一直在尝试让 OAUTH2 应用程序仅进行身份验证,但是当我发送消费者 key 和密码时,我收到错误 400 响应。

代码如下 - 任何帮助将不胜感激。

HttpClient httpclient = new DefaultHttpClient();
uriString = "https://api.twitter.com/oauth2/token";
HttpPost httppost = new HttpPost(uriString);
HttpParams httpParams = httppost.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);

String base64EncodedString =null;
try {
String encodedConsumerKey = URLEncoder.encode("twitter_consumer_key","UTF-8");
String encodedConsumerSecret = URLEncoder.encode("twitter_consumer_secret","UTF-8");
String authString = encodedConsumerKey +":"+encodedConsumerSecret;
base64EncodedString = Base64.encodeToString(authString.getBytes("UTF-8"), Base64.DEFAULT);
} catch (Exception ex) {
//do nothing for now...
}

httppost.setHeader(AUTHORIZATION, "Basic " + base64EncodedString);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
HttpResponse response =null;

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("grant_type", "client_credentials"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
response = httpclient.execute(httppost);

statusCode = response.getStatusLine().getStatusCode();

最佳答案

看起来问题是将字符串编码为 Base64 - 我需要 Base64.NO_WRAP 而不是 Base64.DEFAULT,因为这会将字符串分为两行。

HttpClient httpclient = new DefaultHttpClient();
uriString = "https://api.twitter.com/oauth2/token";
HttpPost httppost = new HttpPost(uriString);
HttpParams httpParams = httppost.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);

String base64EncodedString =null;
try {
String encodedConsumerKey = URLEncoder.encode("twitter_consumer_key","UTF-8");
String encodedConsumerSecret = URLEncoder.encode("twitter_consumer_secret","UTF-8");
String authString = encodedConsumerKey +":"+encodedConsumerSecret;
base64EncodedString = Base64.encodeToString(authString.getBytes("UTF-8"), Base64.NO_WRAP); //Changed here!!!
} catch (Exception ex) {
//do nothing for now...
}

httppost.setHeader(AUTHORIZATION, "Basic " + base64EncodedString);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
HttpResponse response =null;

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("grant_type", "client_credentials"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
response = httpclient.execute(httppost);

statusCode = response.getStatusLine().getStatusCode();

关于android - 从 Android 到 twitter API 的 Oauth2 连接(仅应用程序身份验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17152700/

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