我正在使用OAuth消费者 http://code.google.com/p/oauthconsumer/在我的 iOS 应用程序中,它使用汤不热API http://www.tumblr.com/docs/en/api/v2#posting。一般来说,进行 API 调用效果很好。但是,我很难上传任何媒体。当我的请求的所有参数都是整数或字符串时,我像这样添加它们:
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"x_auth_username" value:username],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:password],
nil]];
这显然不适用于例如图片。
我发现我可能必须将这些数据发送为multipart/form-data
代替application/x-www-form-urlencoded
因此,它不会对 oAuth 签名产生任何影响。但是,据我所知,OAuthConsumer 仅支持x-www-form-urlencoded
(相关代码位于NSMutableURLRequest+Parameters.m
)。但是,我不确定这是否正确,如果是,我真的不知道如何修改Consumer
正确。任何帮助,将不胜感激!
好吧,我自己想出来了。这有几个部分,因为我看到其他人也有类似的问题,所以我将详细介绍:
首先,我使用的是过时版本的 OAuthConsumer。而不是使用以下版本链接到 Google 代码 http://code.google.com/p/oauthconsumer/,您应该使用更新的版本来自 github https://github.com/jdg/oauthconsumer因为它包括发送包含多个字符串的多部分表单的方法。
现在,如果我没有完全错的话,理论上你现在应该做的是:
//Setup the request...
[request setParameters:params];
[request attachFileWithName:@"data" filename:@"photo.jpg" contentType:@"image/jpeg" data:dataProp.data];
//Setup the fetcher and send the request...
这将生成一个 oAuth 签名,其中仅包含oauth_...
-variables,将所有其他变量放入多部分形式中。事情应该是这样的,根据文档,你应该没问题。不幸的是,你不是,tumblr 将返回 401 错误,很可能是由于无效签名造成的。
这是你真正需要做的:
//Setup the request...
[request setParameters:params];
[request prepare]; //Whaaaat?
[request attachFileWithName:@"data" filename:@"photo.jpg" contentType:@"image/jpeg" data:dataProp.data];
//Setup the fetcher, make sure "prepare" isn't called again, send the request...
这会起作用...同样,我很确定这不是 oAuth 应该处理此问题的方式,但至少它起作用了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)