【问题标题】:Having problems with uploading photos to TwitPic using OAuth in Objective C on the iPhone在 iPhone 上的 Objective C 中使用 OAuth 将照片上传到 TwitPic 时遇到问题
【发布时间】:2011-05-06 20:15:36
【问题描述】:

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

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

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

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

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

Example iPhone app using OAuth

这是我的代码:

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"

【问题讨论】:

    标签: iphone upload oauth photo twitpic


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      一切准备就绪?

      【讨论】:

      • 在哪里可以找到令牌?你能提供我正在使用你的 GSTwitPicEngine 的例子吗
      • 大约 3 年前,我使用了 ASIHTTPRequest。在当时,它当然具有易用性的好处。不过我很久以前就离开了。
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多