Phl*_*bbo 3 http oauth tumblr ios
我在我的iOS应用程序中使用OAuthConsumer,它使用了Tumblr API.使API调用一般工作正常.但是,我很难上传任何媒体.当我的请求的所有参数都是int或字符串时,我就像这样添加它们:
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"x_auth_username" value:username],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:password],
nil]];
Run Code Online (Sandbox Code Playgroud)
这显然不适用于例如图像.我想通了,我可能要为发送此数据multipart/form-data代替application/x-www-form-urlencoded,因此,它不会对OAuth的签名任何影响.但是,据我所知,OAuthConsumer仅支持x-www-form-urlencoded(相关代码位于其中NSMutableURLRequest+Parameters.m).但是,我不确定这是否正确,如果是这样,我真的不知道如何Consumer正确修改.任何帮助,将不胜感激!
好吧,我想通了我自己.这有几个部分,因为我看到其他人有类似的问题,我将全面介绍:
首先,我使用的是OAuthConsumer的过时版本.您应该使用github中的更新版本,而不是使用Google代码上链接的版本,因为它包含了发送包含多个字符串的多部分表单的方法.
现在,如果我不是完全错了,理论上你现在应该做的是:
//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...
Run Code Online (Sandbox Code Playgroud)
这将生成一个oAuth签名,其中仅包含-variables oauth_...,将所有其他变量放入multipart表单中.这应该是它应该如何,根据文档,你应该没事.不幸的是,你不是,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...
Run Code Online (Sandbox Code Playgroud)
这将有效...再次,我很确定这不是oAuth应该如何处理这个,但至少它是有效的.
| 归档时间: |
|
| 查看次数: |
2522 次 |
| 最近记录: |