在 iPhone 上使用 Objective C 中的 OAuth 将照片上传到 TwitPic 时遇到问题
我一直在开发一款 iPhone 应用程序,它具有将照片上传到 TwitPic 的功能。我让它与基本身份验证一起使用。
我正在尝试让它与 OAuth 一起使用。我收到身份验证错误。我非常仔细地研究了 TwitPic 文档。
我通过显示 UI Web 视图来授权该应用程序,并且它返回 PIN 值。我在应用程序中输入 PIN 值并请求令牌。
我可以将状态更新上传到 Twitter,但不能上传照片。
我的代码基于此处的一些示例代码:
这是我的代码:
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"
I have been working on an iPhone app that has a feature of uploading photos to TwitPic. I have it working with basic authentication.
I am trying to get it working with OAuth. I am getting authentication errors. I have studied very carefully the TwitPic documentation.
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.
I am able to upload status updates to Twitter but not photos.
My code is based on some example code from here:
Example iPhone app using OAuth
Here is my code:
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];
Here is the OAuth Header:
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哈!我找到了!
我们应该使用 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)
使用GSTwitPicEngine:https://github.com/Gurpartap/GSTwitPicEngine
使用GSTwitPicEngine :
使用类或根据需要初始化引擎:
查找授权令牌并提供给 twitpicEngine:
然后上传图像并附加文本消息(不发布到 Twitter):
仅上传图像:
请求结束时,使用适当的数据和信息调用委托方法之一。
GSTwitPicEngineDelegate 协议指定了两个委托方法:
和
All set?
Use GSTwitPicEngine: https://github.com/Gurpartap/GSTwitPicEngine
Using GSTwitPicEngine:
Initialize the engine with class or as needed:
Find the authorization token and supply to twitpicEngine with:
Then to upload image and attach a text message along with it (does not post to twitter):
To upload image only:
Upon end of request, one of the delegate methods is called with appropriate data and information.
GSTwitPicEngineDelegate protocol specifies two delegate methods:
and
All set?
生成标头的 OAuth 方法必须是 GET。不是邮寄。
另外,网址必须为 https://api.twitter.com/1/account/verify_credentials。 json
OAuth method to generate a header must be GET. Not POST.
Also url must be https://api.twitter.com/1/account/verify_credentials.json
谢谢,这也帮助我让它工作:)我还更新了 http://github.com/jaanus/PlainOAuth< /a> 带有工作示例代码。
Thanks, this helped me get it working too :) I also updated http://github.com/jaanus/PlainOAuth with working example code.