使用OAuthConsumer for iOS将图像上传到tumblr [英] Uploading Image to tumblr using OAuthConsumer for iOS

查看:148
本文介绍了使用OAuthConsumer for iOS将图像上传到tumblr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS应用程序中使用 OAuthConsumer ,该应用程序使用 Tumblr API 。使API调用一般工作正常。但是,我很难上传任何媒体。当我的请求的所有参数都是整数或字符串时,我就像这样添加它们:

I'm using the OAuthConsumer in my iOS application which makes use of the Tumblr API. Making API-Calls in general works fine. However, I struggle to upload any media. When all the parameters of my requests are ints or strings, I add them just like that:

    [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 。任何帮助将不胜感激!

That obviously won't work for e.g. images. I figured out that I probably will have to send this data as multipart/form-data instead of application/x-www-form-urlencoded and therefore, it won't have any effect on the oAuth signature. However, as far as I can tell, the OAuthConsumer only supports x-www-form-urlencoded (with the relevant code lying in the NSMutableURLRequest+Parameters.m). However, I'm not sure whether this is correct and, if so, I don't really know how to modify the Consumer correctly. Any help would be appreciated!

推荐答案

好吧,我想出了我自己。这有几个部分,因为我看到其他人有类似的问题,我会全长:

Ok, I figured it out my self. There are several parts to this and since I saw other people having similar questions, I'll go into full length:

首先,我使用的是过时的版本OAuthConsumer。您应该使用更新版本<而不是使用链接到Google代码的版本<来自github 的href =https://github.com/jdg/oauthconsumer =noreferrer>因为它包含了发送包含多个字符串的多部分表单的方法。

First of all, I was using an outdated version of OAuthConsumer. Instead of using the version that is linked on Google Code, you should use the more recent version from github since it includes the means to send a multipart form with more than strings in it.

现在,如果我没有完全错,理论上你现在应该做的是:

Now, if I'm not completely wrong, what you should theoretically do now is the following:

//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错误,最有可能是签名无效。

This will generate an oAuth signature which includes only the oauth_...-variables, putting all your other variables into the multipart form. This is how it should be and according to the documentation, you should be fine. Unfortunately, you aren't, tumblr will return a 401 error, resulting most likely from an invalid signature.

这是你真正需要做的事情:

Here is what you really have to do:

//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应该如何处理这个,但至少它可行。

This will work... Again, I'm pretty sure that this is not how oAuth is supposed to handle this but at least it works.

这篇关于使用OAuthConsumer for iOS将图像上传到tumblr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆