在iPhone上的Objective C中使用OAuth将照片上传到TwitPic时遇到问题 [英] Having problems with uploading photos to TwitPic using OAuth in Objective C on the iPhone

查看:160
本文介绍了在iPhone上的Objective C中使用OAuth将照片上传到TwitPic时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一款iPhone应用程序,它具有将照片上传到TwitPic的功能。我使用它进行基本身份验证。

I have been working on an iPhone app that has a feature of uploading photos to TwitPic. I have it working with basic authentication.

我正在尝试使用OAuth。我收到身份验证错误。我已经仔细研究过TwitPic文档。

I am trying to get it working with OAuth. I am getting authentication errors. I have studied very carefully the TwitPic documentation.

我通过显示UI Web View来授权应用程序并返回PIN值。我在应用程序中输入PIN值并请求令牌。

I am authorising the app by displaying a UI Web View and the it returns a PIN value. I enter the PIN value in the app and request the token.

我可以将状态更新上传到Twitter但不上传照片。

I am able to upload status updates to Twitter but not photos.

我的代码基于此处的一些示例代码:

My code is based on some example code from here:

使用OAuth的iPhone应用示例

这是我的代码:

NSString *url = @"http://api.twitpic.com/2/upload.json";
NSString *oauth_header = [oAuth oAuthHeaderForMethod:@"POST" andUrl:url andParams:nil];

NSLog(@"OAuth header : %@\n\n", oauth_header);

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
request.requestMethod = @"POST";

[request addRequestHeader:@"X-Auth-Service-Provider" value:@"https://api.twitter.com/1/account/verify_credentials.json"];   

[request addRequestHeader:@"X-Verify-Credentials-Authorization" value:oauth_header];    

NSData *imageRepresentation = UIImageJPEGRepresentation(imageToUpload, 0.8);        
[request setData:imageRepresentation forKey:@"media"];
[request setPostValue:@"Some Message"  forKey:@"message"];  
[request setPostValue:TWITPIC_API_KEY  forKey:@"key"];  

[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestFailed:)];

[request start];    

这是OAuth标题:

OAuth realm="http://api.twitter.com/", oauth_timestamp="1275492425", oauth_nonce="b686f20a18ba6763ac52b689b2ac0c421a9e4013", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="zNbW3Xi3MuS7i9cpz6fw", oauth_version="1.0", oauth_token="147275699-jmrjpwk3B6mO2FX2BCc9Ci9CRBbBKYW1bOni2MYs", oauth_signature="d17HImz6VgygZgbcp845CD2qNnI%3D"


推荐答案

哈!我找到了!
我们应该使用 https:/创建标题 /api.twitter.com/1/account/verify_credentials.json 并发布到 http ://api.twitpic.com/2/upload.json ! (并使用GET)

HA! I found it! We should create the header with https://api.twitter.com/1/account/verify_credentials.json and post to http://api.twitpic.com/2/upload.json! (And use GET)

    NSString *fakeurl = @"https://api.twitter.com/1/account/verify_credentials.json";
NSString *oauth_header = [oAuth oAuthHeaderForMethod:@"GET" andUrl:fakeurl andParams:nil];

NSLog(@"OAuth header : %@\n\n", oauth_header);

NSString *url = @"http://api.twitpic.com/2/upload.json";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
request.delegate = self;
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
request.requestMethod = @"GET";

[request addRequestHeader:@"X-Verify-Credentials-Authorization" value:oauth_header];    
[request addRequestHeader:@"X-Auth-Service-Provider" value:@"https://api.twitter.com/1/account/verify_credentials.json"];   


NSData *imageRepresentation = UIImageJPEGRepresentation([UIImage imageNamed:@"IMG_0717.jpg"], 0.2);    
if (imageRepresentation) {
    NSLog(@"Pic not nil");
}
[request setData:imageRepresentation forKey:@"media"];
[request setPostValue:@"twitpic, i hate you. die painfully."  forKey:@"message"];  
[request setPostValue:twitPicKey  forKey:@"key"];  

[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestFailed:)];

[request start];

这篇关于在iPhone上的Objective C中使用OAuth将照片上传到TwitPic时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆