我正在使用jTwitter for android开发我的应用程序。我能够将用户重定向到授权页面,在那里他输入凭据以授权应用程序。然后调用回调函数。在回调函数中,我无法获取访问令牌。这让我有例外。请帮助。
收到“与服务提供商的通信失败”异常。 更新:我得到令牌但仍然得到例外。 这是代码:
Twitter twitter = null;
private EditText tweetTxt = null;
private Button postBtn = null;
private OAuthSignpostClient client = null;
private String CALLBACK_URI = "myapp://twitt";
private String authUrl = null;
private ProgressDialog postDialog = null;
String[] accessTokenandSecret=null;
String consumerKey="";
String consumerSecret="";
String verifier;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postBtn=(Button)findViewById(R.id.button1);
postBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyTask().execute();
}
});
Button postTweet = (Button)findViewById(R.id.button2);
postTweet.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
new PostTweet().execute();
}
});
}
private class PostTweet extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "pre execute 2", Toast.LENGTH_LONG).show();
if(accessTokenandSecret==null)
{
Toast.makeText(getApplicationContext(), "IT IS NULL", Toast.LENGTH_LONG).show();
}
}
@Override
protected Void doInBackground(Void... arg0) {
client=new OAuthSignpostClient(consumerKey, consumerSecret, accessTokenandSecret[0], accessTokenandSecret[1]);
Twitter jtwit = new Twitter(null, client);
jtwit.setStatus("test post from my app");
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "Tweet is post Successfully!!", Toast.LENGTH_SHORT).show();
}
}
private class MyTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "pre execute", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
client = new OAuthSignpostClient("QvkiReKCHNKcHn3pGrEzQ" ,"EV9vdMRfuT2AQlSNPJW4LhDAyOe0z1mAZYqHJNkPH7g", CALLBACK_URI);
authUrl = client.authorizeUrl().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "post execute", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
//Check if you got NewIntent event due to Twitter Call back only
if (uri != null && uri.toString().startsWith(CALLBACK_URI))
{
try
{
verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Toast.makeText(getApplicationContext(), "inside verfier", Toast.LENGTH_LONG).show();
client.setAuthorizationCode(verifier);
accessTokenandSecret=client.getAccessToken();
//if(accessTokenandSecret==null)
//{
//Toast.makeText(getApplicationContext(), "it is null you fool", Toast.LENGTH_LONG).show();
// }
//client=new OAuthSignpostClient(consumerKey, consumerSecret, accessTokenandSecret);
}
catch(Exception e){
Log.d("exception", " "+e.getMessage());
}
}}
日志cat错误:
02-17 15:13:46.599: E/AndroidRuntime(12915): FATAL EXCEPTION: AsyncTask #4
02-17 15:13:46.599: E/AndroidRuntime(12915): java.lang.RuntimeException: An error occured while executing doInBackground()
02-17 15:13:46.599: E/AndroidRuntime(12915): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 15:13:46.599: E/AndroidRuntime(12915): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 15:13:46.599: E/AndroidRuntime(12915): at java.lang.Thread.run(Thread.java:856)
02-17 15:13:46.599: E/AndroidRuntime(12915): Caused by: winterwell.jtwitter.TwitterException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: http://twitter.com/oauth/access_token
02-17 15:13:46.599: E/AndroidRuntime(12915): at winterwell.jtwitter.OAuthSignpostClient.setAuthorizationCode(OAuthSignpostClient.java:431)
02-17 15:13:46.599: E/AndroidRuntime(12915): at com.example.retrieve.contacts.twitter.MainActivity$GetAccessTok.doInBackground(MainActivity.java:114)
答案 0 :(得分:1)
您可以尝试从http://winterwell.com/software/jtwitter.php下载最新版本的JTwitter吗? AndroidTwitterLogin中存在一个错误,其中未使用callback-url。现在已经修补了。
顺便说一下 - 小心在网上发布你的应用程序的oauth细节。一旦您的应用程序正常运行,我建议您访问dev.twitter.com并生成新的详细信息。