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"
4

4 回答 4

3

哈!我找到了!我们应该使用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];
于 2010-06-15T08:05:09.227 回答
3

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

使用 GSTwitPicEngine:

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

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

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

[twitpicEngine setAccessToken:token];

然后上传图片并附上一条短信(不发布到推特):

[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");
  }
}

可以了,好了?

于 2011-05-06T12:15:36.217 回答
0

生成标头的 OAuth 方法必须是 GET。不是 POST。

url 也必须是https://api.twitter.com/1/account/verify_credentials.json

于 2010-06-10T14:17:16.403 回答
0

谢谢,这也帮助我让它工作:) 我还用工作示例代码更新了http://github.com/jaanus/PlainOAuth 。

于 2010-07-04T08:28:27.727 回答