我正在尝试将twitter集成到android.I作为解析用户登录到twitter但无法更新我的状态,因为我现在收到禁用的403错误。这可能是403错误的原因以及如何纠正它?任何帮助将不胜感激.. 我的代码是
public void twitterbtnClick(View v)
{
ParseTwitterUtils.logIn(this, new LogInCallback() {
@Override
public void done(ParseUser arg0, com.parse.ParseException arg1) {
// TODO Auto-generated method stub
if (arg0 == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (arg0.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
TweetsTask task = new TweetsTask();
task.execute("Hi buddies");
} else {
Log.d("MyApp", "User logged in through Twitter!");
TweetsTask task = new TweetsTask();
task.execute("Hi buddies");
}
}
});
}
public class TweetsTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.d("Parameter",""+params[0]);
HttpClient client =new DefaultHttpClient();
String status = params[0];
Log.d("Status",""+status);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("mystring", status));
Log.d("Parameter2",""+status);
HttpPost httppost = new HttpPost("https://api.twitter.com/1.1/statuses/update.json");
try {
httppost.setEntity(new UrlEncodedFormEntity(parameters));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ParseTwitterUtils.getTwitter().signRequest(httppost);
try {
HttpResponse response = client.execute(httppost);
if(response==null){
task="fail";
}else{
task="success";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return task;
}
}
protected void onPostExecute(String result) {
if(task.equals("success"))
{
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText( getApplicationContext()," error",Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
我没有足够的声誉来评论user2853201的问题,而是通过查看代码。 NameValuePair参数&#34; mystring&#34;在代码中应更改为&#34; status&#34;。从Twitter文档中,&#34; 403&#34;当您尝试发送重复的推文时会发生错误。
以下是我使用Parse发送推文的方式:
这是我的twitter按钮的OnClickListener()方法:
btnTwitter.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
ShareViaTwitter();
}
});
ShareViaTwitter()方法:
private void displayShareTwitter (){
ParseTwitterUtils.logIn(this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException arg1) {
// TODO Auto-generated method stub
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (user.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
new sendTweet().execute("your twitter status update");
} else {
Log.d("MyApp", "User logged in through Twitter!");
new sendTweet().execute("your twitter status update");
}
}
});
}
sendTweet内部类:
class sendTweet extends AsyncTask<String, Void, String> {
boolean success = true;
@Override
protected String doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
String status = params[0];
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
//This is where you add all the parameters you need for your tweet,
//see "https://dev.twitter.com/docs/api/1/post/statuses/update"
//for all the optional parameters you can add here
parameters.add(new BasicNameValuePair("status", status));
HttpPost httppost = new HttpPost("https://api.twitter.com/1.1/statuses/update.json"); //API used here 1.1
try {
httppost.setEntity(new UrlEncodedFormEntity(parameters));
} catch (UnsupportedEncodingException e) {
success = false;
e.printStackTrace();
}
ParseTwitterUtils.getTwitter().signRequest(httppost);//This seems to cause the app to ask for authorization each time I tweet
HttpResponse response = client.execute(httppost);
} catch (Exception e) {
success = false;
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
if (success){
Toast.makeText(getApplicationContext(),"sent tweet",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"tweet failed",Toast.LENGTH_SHORT).show();
}
}
}