我可以使用 oauth 成功登录 Twitter。现在我需要发布带有状态的图像。为此,我实现了以下..
-(void)shareontw{
NSString *postUrl =@"https://api.twitter.com/1.1/statuses/update_with_media.json";
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postUrl]];
[req setValue:[oAuth oAuthHeaderForMethod"OST" andUrl:postUrl andParams:nil] forHTTPHeaderField"Authorization"];
[req setHTTPMethod"OST"];
NSString *boundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat"multipart/form-data; boundary=%@",
boundary];
[req addValue:headerBoundary forHTTPHeaderField"Content-Type"];
NSMutableData *myRequestData = [NSMutableData data];
NSData *imageData = UIImageJPEGRepresentation(globalimage, 0.8);
[myRequestData appendData:[[NSString stringWithFormat"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"dummy.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// add it to body
[myRequestData appendData:imageData];
[myRequestData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[[NSString stringWithFormat"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Disposition: form-data; name=\"message\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Success\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[[NSString stringWithFormat"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:myRequestData];
NSHTTPURLResponse *response;
NSError *error = nil;
NSString *responseString = [[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error] encoding:NSUTF8StringEncoding] autorelease];
if (error) {
NSLog(@"Error from NSURLConnection: %@", error);
}
NSLog(@"Got HTTP status code from Twitter after posting profile image: %d", [response statusCode]);
NSLog(@"Response string: %@", responseString);}
但它给了我错误:响应字符串:{"errors":[{"message":"Bad Authentication data","code":215}]}
我无法找到我在这里做错的问题。如果有人有想法,请帮助我。
提前致谢。
感谢您上面的代码运行良好,为我节省了很多时间。我只需更改一行代码
这一行 NSData *imageData = UIImageJPEGRepresentation(globalimage, 0.8);
替换为
NSData *imageData = UIImageJPEGRepresentation(_imgView.image, 0.8);
它正在工作
关于ios - 通过 oauth 将图像发送到 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042700/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |