我正在使用twitter4j,我能够成功登录我的Twitter帐户,但是当我试图发布一些内容时,会引发以下异常。
我的代码
try
{
Bitmap bmpFinal = getBitmapFromURL(Imagename.trim());
Log.d("ImagePath",Imagename);
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(access_token)
.setOAuthAccessTokenSecret(access_token_secret)
.setMediaProviderAPIKey("320954b8bf13ffd7463a4dbe667b9dd2")
.build();
Uri tempUri = getImageUri(getApplicationContext(), bmpFinal);
File finalFile = new File(getRealPathFromURI(tempUri));// THIS IS IMPORTANT. TWITPIC NEEDS THE ACTUAL / ABSOLUTE PATH OF THE IMAGE. JUST THE URI DOES NOT WORK!!!!
ImageUpload upload = new ImageUploadFactory(conf).getInstance(MediaProvider.TWITPIC);
String url = upload.upload(finalFile);
Log.e("TWITTER URL RESPONSE", url);
Log.d("TWITTER URL RESPONSE", url);
// END OF FIRST STEP:
// SECOND STEP IS TO UPLOAD TO TWITTER
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(access_token, access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
String finalStatusWithURL = null;
if (status.trim().length() > 0) {
finalStatusWithURL = status + ":\n " + url;
} else {
finalStatusWithURL = url;
}
twitter4j.Status response = twitter.updateStatus(finalStatusWithURL);
Log.e("TWITTER RESPONSE", response.getText());
}
catch(Exception e)
{
Log.d("Exception ",""+e);
}
return null;
}
Logcat异常
06-14 12:51:31.117: D/Exception(3302): No authentication challenges found
06-14 12:51:31.117: D/Exception(3302): Relevant discussions can be found on the Internet at:
06-14 12:51:31.117: D/Exception(3302): http://www.google.co.jp/search?q=ea09dc6d or
06-14 12:51:31.117: D/Exception(3302): http://www.google.co.jp/search?q=051c1797
06-14 12:51:31.117: D/Exception(3302): TwitterException{exceptionCode=[ea09dc6d-051c1797 0237e8c8-9e66d70f], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.2}
答案 0 :(得分:1)
twitter = new TwitterFactory().getInstance();
//Retrieve access token
authProvider.retrieveAccessToken(authConsumer,verifier);
//Initialize access token of twitter4j
AccessToken mAccessToken = new AccessToken( authConsumer.getToken(),authConsumer.getTokenSecret());
//optional to save it for future use
ProjectUtil.setAccessTokenTwitter((MainActivity)mScreen, mAccessToken.getToken());
ProjectUtil.setAccessSecretTwitter((MainActivity)mScreen, mAccessToken.getTokenSecret());
//Set twitter4j.Twitter Oauth
twitter.setOAuthConsumer(AppConstants.CONSUMER_KEY, AppConstants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(mAccessToken);
//message
StatusUpdate ad=new StatusUpdate("my messgae");
//Image from sdcard
File file = new File(Environment.getExternalStorageDirectory()+"/myimage.png");
InputStream inputStream = new FileInputStream(file);
ad.setMedia("myMedia",inputStream);
twitter4j.Status response = twitter.updateStatus(ad);
Log.d("Status", response.getText());