我们可以使用仅限app的身份验证来获取Android中的Twitter趋势 请帮帮我
我得到的错误认证错误215
继承我的代码
我只需要了解oauth
验证活动类
package com.tmm.android.twitter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import com.google.gson.Gson;
import twitter4j.http.BASE64Encoder;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AuthActivity extends Activity {
private Button buttonLogin;
String bearer;
@Override
public void onCreate(Bundle savedInstanceState) {
//System.setProperty("http.keepAlive", "false");
super.onCreate(savedInstanceState);
setContentView(R.layout.main_oauth);
//Define login button and listener
buttonLogin = (Button)findViewById(R.id.ButtonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
askOAuth();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
getjson();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void askOAuth() throws ClientProtocolException, IOException {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("https://api.twitter.com/oauth2/token");
final String APIKEY = "Y0VtCZrmBYFAlMOIjzU4w"; //Consumer
final String APISECRET = "6iYDIgTJjUjfx4RJ2zMkrrLY6tvI0F4oobRcFWdTk"; //Consumer Secret
String apiString = APIKEY + ":" + APISECRET;
String authorization = "Basic " + BASE64Encoder.encode(apiString.getBytes()).toString();
Log.d("auth authorization",authorization);
httppost.setHeader("Authorization", authorization);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(new StringEntity("grant_type=client_credentials"));
InputStream inputStream = null;
Log.d("auth response",httpclient.execute(httppost).toString());
org.apache.http.HttpResponse response = httpclient.execute(httppost);
//Log.d(" auth entity",response.getEntity().toString());
HttpEntity entity = response.getEntity();
//Log.d("auth entitycontent",entity.getContent().toString());
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
bearer=sb.toString();
}
public void getjson() throws ClientProtocolException, IOException, org.json.JSONException
{
//JSONArray jarray;
Gson gson =new Gson();
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpGet httpget = new HttpGet("https://api.twitter.com/1.1/trends/place.json?id=1");
httpget.setHeader("Authorization", "bearer");
httpget.setHeader("Content-type", "application/json");
InputStream inputStream = null;
Log.d("auth response",httpclient.execute(httpget).toString());
org.apache.http.HttpResponse response = httpclient.execute(httpget);
//Log.d(" auth entity",response.getEntity().toString());
HttpEntity entity = response.getEntity();
//Log.d("auth entitycontent",entity.getContent().toString());
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
Log.e("sb", sb.toString());
TwitterTrends objs=gson.fromJson(sb.toString(), TwitterTrends.class);
Log.e("MY INFO", ""+objs.getTrends().size());
for(TwitterTrend tr : objs.getTrends()){
Log.d("TRENDS NAME", tr.getName());
}
}
}
ive twiiter trend class和twitter trend class
不要担心gson部分
我得到的错误认证错误215
答案 0 :(得分:0)
您不做的是让用户登录他的Twitter帐户。确保用户首先登录。登录 - 代码将验证签名,然后才会删除您的错误身份验证错误。