微博update_with_media通过抄写的OAuth在Android上 [英] Twitter update_with_media Via Scribe OAuth On Android
问题描述
我目前使用抄写既验证和后非媒体消息,Twitter的成功。这是很容易的,我的第一次测试的消息公布没有问题。不过,我似乎无法上传照片的。我已经审查了与媒体,无论这里和发布微博的说明这里。
所有的在Github上抄写/ Twitter的例子是对于非媒体的职位。这将是巨大的,如果有人能提供如何通过抄写照片发布到微博了坚实的例子!
我有两个问题,尤其是:
1)我的帖子将无法通过认证。我试着模仿我张贴上面的例子,但似乎没有任何工作。
2)当从byte []的图像转换为字符串,我只能似乎得到4113个字符之前停止。从我的理解,这是非常字符的字符串可以容纳数下。
下面是我如何提取照片:
//获取照片文件和文件长度
//实例进行上传,文件长度变
档案文件=新的文件(照片); //(照片是一个字符串路径照片文件)
INT文件长度=(INT)file.length();
uploadFile =新的字节[文件长度]
//创建文件缓冲输入流
的BufferedInputStream的InputStream;
尝试{的InputStream =新的BufferedInputStream(新的FileInputStream(文件));}
赶上(FileNotFoundException异常E)
{
的InputStream = NULL;
Toast.makeText(this.getApplicationContext(),缓冲输入流的错误!,Toast.LENGTH_LONG).show();
}
//读数据从文件到上传变量
//关闭输入流
尝试{inputStream.read(uploadFile);}
赶上(IOException异常E){Toast.makeText(this.getApplicationContext(),读取输入流上传变量错误!,Toast.LENGTH_LONG).show();}
尝试{inputStream.close();}
赶上(IOException异常E){Toast.makeText(this.getApplicationContext(),关闭输入流的错误!,Toast.LENGTH_LONG).show();}
来自不同的地方,我终于想通了我在做什么错了研究和一块铣code很多之后。下面是如何通过抄写OAuth的照片发布到Twitter的一个例子:
请注意:这是假定一些事情...
1)你已经保存了照片,并有文件路径
2)您已经验证在某些时候用户并有一个有效的访问令牌
3)必须添加的apache-mime4j-0.6.jar和放大器; httpmime-4.0.1.jar到你的库文件夹,包括他们在您的构建路径!
我真的希望这可以帮助别人!这是很容易实现,但采取了排除故障的几天得到它正常工作!
// BUILD OAUTH服务
OAuthService的OAuth =新ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(YOUR_TWITTER_API_KEY)//更换你自己的!
.apiSecret(YOUR_TWITTER_API_SECRET)//更换你自己的!
.callback(YOUR_CALLBACK)//更换你自己的!
。建立();
// BUILD OAUTH请求和放大器;报名马上(否则MULTIPART形式可以preVENT SIGNING)
OAuthRequest请求=新OAuthRequest(Verb.POST,https://upload.twitter.com/1/statuses/update_with_media.json);
oAuth.signRequest(USER_ACCESS_TOKEN,请求); // ENTER用户的访问令牌
//增加多重表单
尝试
{
MultipartEntity实体=新MultipartEntity();
entity.addPart(状态,新StringBody(消息)); //这是Twitter信息
entity.addPart(媒体,新FileBody(新文件(照片))); //这是上传照片
ByteArrayOutputStream OUT =新ByteArrayOutputStream();
entity.writeTo(出);
request.addPayload(out.toByteArray());
request.addHeader(entity.getContentType()的getName(),entity.getContentType()的getValue());
}
赶上(UnsupportedEncodingException E){e.printStackTrace();}
赶上(IOException异常E){e.printStackTrace();}
// 发送请求
尝试{响应=新的JSONObject(request.send()getBody());}
赶上(JSONException E){Log.e(YOUR_APP_TAG,JSONException抛出:+ e.getMessage());}
I'm currently using Scribe to both authenticate and post non-media messages to Twitter successfully. This was very easy, my first test message posted with no issues. However, I can't seem to post photos at all. I have reviewed Twitter's instructions for posting with media, both here and here.
All of the Scribe/Twitter examples at Github are for non-media posts. It would be great if someone could provide a solid example of how to post photos to Twitter via Scribe!
I'm have two issues in particular:
1) My posts will not pass authorization. I've tried mimicking the examples I posted above, but nothing seems to work.
2) When converting the image from byte[] to a string, I only seem to get 4113 characters before it stops. From my understanding, this is well under the number of characters a String can hold.
Here is how I'm extracting the photo:
// GET PHOTO FILE AND FILE LENGTH
// INSTANTIATE UPLOAD VARIABLE WITH FILE LENGTH
File file = new File(photo); // ("photo" is a string path to the photo file)
int fileLength = (int) file.length();
uploadFile = new byte[fileLength];
// CREATE BUFFER INPUT STREAM OF FILE
BufferedInputStream inputStream;
try {inputStream = new BufferedInputStream(new FileInputStream(file));}
catch (FileNotFoundException e)
{
inputStream = null;
Toast.makeText(this.getApplicationContext(), "Buffer input stream error!", Toast.LENGTH_LONG).show();
}
// READ DATA FROM FILE INTO UPLOAD VARIABLE
// CLOSE INPUT STREAM
try {inputStream.read(uploadFile);}
catch (IOException e) {Toast.makeText(this.getApplicationContext(), "Read input stream to upload variable error!", Toast.LENGTH_LONG).show();}
try {inputStream.close();}
catch (IOException e) {Toast.makeText(this.getApplicationContext(), "Close input stream error!", Toast.LENGTH_LONG).show();}
After a LOT of research and piece milling code from various places I finally figured out what I was doing wrong. Here is an example of how to post photos to Twitter via Scribe OAuth:
NOTE: This assumes a few things...
1) You have already saved the photo and have the file path
2) You have already authenticated the user at some point and have a valid access Token
3) You MUST add apache-mime4j-0.6.jar & httpmime-4.0.1.jar to you libs folder and include them in your build path!!!
I really hope this helps someone! It's very easy to implement, but took a few days of troubleshooting to get it working correctly!
// BUILD OAUTH SERVICE
OAuthService oAuth = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(YOUR_TWITTER_API_KEY) // REPLACE WITH YOUR OWN!!!
.apiSecret(YOUR_TWITTER_API_SECRET) // REPLACE WITH YOUR OWN!!!
.callback(YOUR_CALLBACK) // REPLACE WITH YOUR OWN!!!
.build();
// BUILD OAUTH REQUEST & SIGN IT RIGHT AWAY (OTHERWISE MULTIPART FORM MAY PREVENT SIGNING)
OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json");
oAuth.signRequest(USER_ACCESS_TOKEN, request); // ENTER USER'S ACCESS TOKEN
// ADD MULTIPART FORM
try
{
MultipartEntity entity = new MultipartEntity();
entity.addPart("status", new StringBody(message)); // THIS IS THE TWITTER MESSAGE
entity.addPart("media", new FileBody(new File(photo))); // THIS IS THE PHOTO TO UPLOAD
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
request.addPayload(out.toByteArray());
request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());
}
catch (UnsupportedEncodingException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
// SEND REQUEST
try {response = new JSONObject (request.send().getBody());}
catch (JSONException e) {Log.e("YOUR_APP_TAG", "JSONException Thrown: " + e.getMessage());}
这篇关于微博update_with_media通过抄写的OAuth在Android上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!