在 iPhone 上使用 Objective C 中的 OAuth 将照片上传到 TwitPic 时遇到问题

发布于 2024-09-03 21:38:06 字数 1861 浏览 8 评论 0原文

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

我正在尝试让它与 OAuth 一起使用。我收到身份验证错误。我非常仔细地研究了 TwitPic 文档。

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

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

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

使用 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"

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亣腦蒛氧 2024-09-10 21:38:06

哈!我找到了!
我们应该使用 https://api.twitter 创建标头 .com/1/account/verify_credentials.json 并发布到 http://api .twitpic.com/2/upload.json! (并使用 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];

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];
万劫不复 2024-09-10 21:38:06

使用GSTwitPicEnginehttps://github.com/Gurpartap/GSTwitPicEngine

使用GSTwitPicEngine :

使用类或根据需要初始化引擎:

self.twitpicEngine = (GSTwitPicEngine *)[GSTwitPicEngine twitpicEngineWithDelegate:self];

查找授权令牌并提供给 twitpicEngine:

[twitpicEngine setAccessToken:token];

然后上传图像并附加文本消息(不发布到 Twitter):

[twitpicEngine uploadPicture:[UIImage imageNamed:@"mypic.png"]  withMessage:@"Hello world!"]; // This message is supplied back in success delegate call in request's userInfo.

仅上传图像:

[twitpicEngine uploadPicture:uploadImageView.image];

请求结束时,使用适当的数据和信息调用委托方法之一。


GSTwitPicEngineDelegate 协议指定了两个委托方法:

- (void)twitpicDidFinishUpload:(NSDictionary *)response {
  NSLog(@"TwitPic finished uploading: %@", response);

  // [response objectForKey:@"parsedResponse"] gives an NSDictionary of the response one of the parsing libraries was available.
  // Otherwise, use [[response objectForKey:@"request"] objectForKey:@"responseString"] to parse yourself.

  if ([[[response objectForKey:@"request"] userInfo] objectForKey:@"message"] > 0 && [[response objectForKey:@"parsedResponse"] count] > 0) {
    // Uncomment to update status upon successful upload, using MGTwitterEngine's instance.
    // [twitterEngine sendUpdate:[NSString stringWithFormat:@"%@ %@", [[[response objectForKey:@"request"] userInfo] objectForKey:@"message"], [[response objectForKey:@"parsedResponse"] objectForKey:@"url"]]];
  }
}

- (void)twitpicDidFailUpload:(NSDictionary *)error {
  NSLog(@"TwitPic failed to upload: %@", error);

  if ([[error objectForKey:@"request"] responseStatusCode] == 401) {
    // UIAlertViewQuick(@"Authentication failed", [error objectForKey:@"errorDescription"], @"OK");
  }
}

All set?

Use GSTwitPicEngine: https://github.com/Gurpartap/GSTwitPicEngine

Using GSTwitPicEngine:

Initialize the engine with class or as needed:

self.twitpicEngine = (GSTwitPicEngine *)[GSTwitPicEngine twitpicEngineWithDelegate:self];

Find the authorization token and supply to twitpicEngine with:

[twitpicEngine setAccessToken:token];

Then to upload image and attach a text message along with it (does not post to twitter):

[twitpicEngine uploadPicture:[UIImage imageNamed:@"mypic.png"]  withMessage:@"Hello world!"]; // This message is supplied back in success delegate call in request's userInfo.

To upload image only:

[twitpicEngine uploadPicture:uploadImageView.image];

Upon end of request, one of the delegate methods is called with appropriate data and information.


GSTwitPicEngineDelegate protocol specifies two delegate methods:

- (void)twitpicDidFinishUpload:(NSDictionary *)response {
  NSLog(@"TwitPic finished uploading: %@", response);

  // [response objectForKey:@"parsedResponse"] gives an NSDictionary of the response one of the parsing libraries was available.
  // Otherwise, use [[response objectForKey:@"request"] objectForKey:@"responseString"] to parse yourself.

  if ([[[response objectForKey:@"request"] userInfo] objectForKey:@"message"] > 0 && [[response objectForKey:@"parsedResponse"] count] > 0) {
    // Uncomment to update status upon successful upload, using MGTwitterEngine's instance.
    // [twitterEngine sendUpdate:[NSString stringWithFormat:@"%@ %@", [[[response objectForKey:@"request"] userInfo] objectForKey:@"message"], [[response objectForKey:@"parsedResponse"] objectForKey:@"url"]]];
  }
}

and

- (void)twitpicDidFailUpload:(NSDictionary *)error {
  NSLog(@"TwitPic failed to upload: %@", error);

  if ([[error objectForKey:@"request"] responseStatusCode] == 401) {
    // UIAlertViewQuick(@"Authentication failed", [error objectForKey:@"errorDescription"], @"OK");
  }
}

All set?

说好的呢 2024-09-10 21:38:06

生成标头的 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

甜心小果奶 2024-09-10 21:38:06

谢谢,这也帮助我让它工作:)我还更新了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文