我正在使用twitpic库在Twitter上发布图像,但我得到了 - 无效的twitpic用户名和密码错误。 这有什么解决方案吗? witpic的用户名和密码是什么。我只是使用我的Twitter ID和密码代替它。
这是我的代码
// Create file
File picture = new File(Environment.getExternalStorageDirectory()+"/image.jpg");
if(picture.exists())
{
System.out.println("Picture accesseds");
}
// Create TwitPic object and allocate TwitPicResponse object
TwitPic tpRequest = new TwitPic("gauravarora90", "gaurav");
TwitPicResponse tpResponse = null;
// Make request and handle exceptions
try {
tpResponse = tpRequest.uploadAndPost(picture, "Hello World!!!");
} catch (IOException e) {
e.printStackTrace();
} catch (TwitPicException e) {
e.printStackTrace();
}
// If we got a response back, print out response variables
if(tpResponse != null) {
tpResponse.dumpVars();
System.out.println(tpResponse.getStatus());
if(tpResponse.getStatus().equals("ok")){
Toast.makeText(getApplicationContext(), "Photo posted on Twitter.",Toast.LENGTH_SHORT).show();
//picture.delete();
}
}
请帮助我。谢谢。
答案 0 :(得分:0)
试试这段代码:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(TwitterClass.CONSUMER_KEY);
cb.setOAuthConsumerSecret(TwitterClass.CONSUMER_SECRET);
cb.setOAuthAccessToken(tw.getAccessToken());
AccessToken a = new AccessToken(tw.getAccessToken(),tw.getSecretToken());
TwitterFactory tf = new TwitterFactory(cb.build());
t4jTwitter = tf.getInstance();
t4jTwitter.setOAuthAccessToken(a);
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(TwitterClass.CONSUMER_KEY)
.setOAuthConsumerSecret(TwitterClass.CONSUMER_SECRET)
.setOAuthAccessToken(tw.getAccessToken())
.setOAuthAccessTokenSecret(tw.getSecretToken())
.build();
OAuthAuthorization auth = new OAuthAuthorization (conf, conf.getOAuthConsumerKey (), conf.getOAuthConsumerSecret (),
new AccessToken (conf.getOAuthAccessToken (), conf.getOAuthAccessTokenSecret ()));
ImageUpload upload = ImageUpload.getTwitpicUploader (TWITPIC_KEY, auth);
try {
// String url = upload.upload(new File("/mnt/sdcard/download/3029302084159350163.jpg"));
//result = 1;
URL myfileUrl=new URL(strFilePath);
HttpURLConnection conn = (HttpURLConnection) myfileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
// String url = upload.upload(new File("/mnt/sdcard/download/3029302084159350163.jpg"), StaticData.strTwitte);
String url = upload.upload(StaticData.strTwitte, is);
System.out.println("Image url"+url);
System.out.println("Image uploaded");
result_img=1;
twitter4j.Status status = t4jTwitter.updateStatus(StaticData.strTwitte+"\n"+url);
int statusId = (int)status.getId();
//Log.d(TAG, "Image uploaded, Twitpic url is " + url);
} catch (Exception e) {
//Log.e(TAG, "Failed to send image");
result_img=0;
System.out.println("Failed to send image");
e.printStackTrace();
}