android中的Twitter api请求并获取响应代码401消息 [英] Twitter api request in android and getting the response code 401 message

查看:107
本文介绍了android中的Twitter api请求并获取响应代码401消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.twitterlayout);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();

StrictMode.setThreadPolicy(policy);

twitter_consumer_key = ;

twitter_consumer_secret =;

oauth_token =;

oauth_token_secret =;

q =印度;

String get_or_post =GET;

//这是用于所有Twitter API调用的签名方法

String oauth_signature_method = HMAC-SHA1;

//生成任何相当随机的字母数字字符串作为nonce。 Nonce =使用的数字ONCE。

字符串uuid_string = UUID.randomUUID()。toString();

uuid_string = uuid_string.replaceAll( - ,);

String oauth_nonce = uuid_string; //任何相对随机的字母数字字符串都可以在这里工作

//获取时间戳

日历tempcal = Calendar.getInstance();

long ts = tempcal.getTimeInMillis(); //以毫秒为单位获取当前时间

字符串oauth_timestamp =(new Long(ts / 1000))。toString(); //然后除以1000来获得秒数

//使用您的消费者密钥组装正确的参数字符串,该字符串必须按字母顺序排列



String parameter_string =lang = en& oauth_consumer_key =+ twitter_consumer_key +& oauth_nonce =+ oauth_nonce +& oauth_signature_method =+ oauth_signature_method +

& oauth_timestamp =+ oauth_timestamp +& oauth_token =+ URLEncoder.encode(oauth_token)+& oauth_version = 1.0& q =+ URLEncoder.encode(q)+& result_type = mixed;





Log.d(parameter_string =,parameter_string);

twitter_endpoint =https://api.twitter.com/1.1 /users/search.json;

twitter_url =https://api.twitter.com/1.1/users/search.json?q=india;



String signature_base_string = get_or_post +&+ URLEncoder.encode(twitter_endpoint)+& + URLEncoder.encode(parameter_string);

String oauth_signature =;

try {

oauth_signature = computeSignature(signature_base_string,twitter_consumer_secret +& amp; ;);

}

catch(GeneralSecurityException e){

e.printStackTrace();

}

catch(UnsupportedEncodingException e){

e.printStackTrace();

}



String authorization_header_string =OAuth oauth_consumer_key = \+ twitter_consumer_key +\,oauth_signature_method = \HMAC-SHA1 \,oauth_timestamp = \+ oauth_timestamp +

\,oauth_nonce = \+ oauth_nonce +\,oauth_version = \1.0 \,oauth_signature = \+ URLEncoder.encode(oauth_signature)+\,oauth_token = \+ URLEncoder.encode(oauth_token)+\;

Lo g.d(Header String,authorization_header_string);



URL url = null;

试试

{

Log.d(url,twitter_url);

url =新网址( twitter_url);

urlConnection =(HttpURLConnection)url.openConnection();

urlConnection.setDoOutput(true);

urlConnection.setDoInput(true );

urlConnection.setRequestMethod(GET);

urlConnection.setRequestProperty(host,api.twitter.com);

urlConnection.setRequestProperty(User-Agent,Mozilla / 5.0);

urlConnection.setRequestProperty(Authorization,authorization_header_string);

urlConnection.setRequestProperty( Content-Type,application / x-www-form-urlencoded);

urlConnection.setUseCaches(false);

int responseCode = urlConnection.getResponseCode() ;

Log.d(响应代码:,String.valueOf(responseCode));

InputStream stream = new BufferedInputStream(urlConnection.getInputStream());

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));

StringBuilder builder = new StringBuilder ();

String responsedata = null;

while((inputString = bufferedReader.readLine())!= null){

builder。 append(inputString);

}



JSONObject topLevel = new JSONObject(jsonCallbackToJson(builder.toString()));

Log.d(Toplevel,builder.toStrin

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitterlayout);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
twitter_consumer_key= "";
twitter_consumer_secret="";
oauth_token = "";
oauth_token_secret = "";
q="India";
String get_or_post = "GET";
// This is the signature method used for all Twitter API calls
String oauth_signature_method = "HMAC-SHA1";
// generate any fairly random alphanumeric string as the "nonce". Nonce = Number used ONCE.
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string; // any relatively random alphanumeric string will work here
// get the timestamp
Calendar tempcal = Calendar.getInstance();
long ts = tempcal.getTimeInMillis();// get current time in milliseconds
String oauth_timestamp = (new Long(ts/1000)).toString(); // then divide by 1000 to get seconds
// assemble the proper parameter string, which must be in alphabetical order, using your consumer key

String parameter_string = "lang=en&oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method +
"&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + URLEncoder.encode(oauth_token) + "&oauth_version=1.0&q=" + URLEncoder.encode(q) + "&result_type=mixed";


Log.d("parameter_string=",parameter_string);
twitter_endpoint = "https://api.twitter.com/1.1/users/search.json";
twitter_url="https://api.twitter.com/1.1/users/search.json?q=india";

String signature_base_string = get_or_post + "&"+ URLEncoder.encode(twitter_endpoint) + "&" + URLEncoder.encode(parameter_string);
String oauth_signature = "";
try {
oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");
}
catch (GeneralSecurityException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

String authorization_header_string = "OAuth oauth_consumer_key=\"" + twitter_consumer_key + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp +
"\",oauth_nonce=\"" + oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + URLEncoder.encode(oauth_signature) + "\",oauth_token=\"" + URLEncoder.encode(oauth_token) + "\"";
Log.d("Header String",authorization_header_string);

URL url = null;
try
{
Log.d("Url",twitter_url);
url = new URL(twitter_url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Host", "api.twitter.com");
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
urlConnection.setRequestProperty("Authorization",authorization_header_string);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setUseCaches(false);
int responseCode = urlConnection.getResponseCode();
Log.d("Response Code :", String.valueOf(responseCode));
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String responsedata = null;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}

JSONObject topLevel = new JSONObject(jsonCallbackToJson(builder.toString()));
Log.d("Toplevel", builder.toStrin


g());

}

catch(MalformedURLException e){

e.printStackTrace();

Log.i(URL-ERROR:,e.toString() );

}

catch(IOException | JSONException e)

{

e.printStackTrace();

Log.i(IO-ERROR:,e.toString() );

}

}



我的尝试:



i已经使用android httpurlconnection尝试了上面的twitter api代码。它给出了401响应代码。请帮助任何人如何制作正确的代码

g());
}
catch (MalformedURLException e) {
e.printStackTrace();
Log.i("URL-ERROR:",e.toString());
}
catch (IOException | JSONException e)
{
e.printStackTrace();
Log.i("IO-ERROR:", e.toString());
}
}

What I have tried:

i have tried the above code for twitter api using android httpurlconnection.It gives 401 response code.please help anyone how to make proper code

推荐答案

请确认您的机器设置时间(时间和地区)。时间不正确可能会导致收到401异常。请注意您的登录凭据。

我也担心您的twitter_endpoint网址是否正确。
Please verify that the time of your machine is correctly setup (both the time and the region). Having an incorrect Time can result in receiving the 401 exception.Also be careful about your login credentials.
I'm also afraid whether your twitter_endpoint url is a right one.


这篇关于android中的Twitter api请求并获取响应代码401消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆