OAuthConsumer 和 Twitpic

发布于 2024-11-05 01:50:04 字数 4154 浏览 0 评论 0原文

我正在尝试使用 OAuthConsumer 将图像发布到 TwitPic。我不断收到 401“无法验证您的身份(标头被 Twitter 拒绝)”。错误。

我还利用 Twitter+OAuth 库来处理登录和发布常规推文,然后保存令牌和令牌秘密以便与 TwitPic 一起使用。

这是我使用构造请求的代码:

NSURL *twitpicUrl = [NSURL URLWithString:@"http://api.twitpic.com/2/upload.json"];
NSString* realm = @"http://api.twitter.com/";
OAToken* oaToken = [[OAToken alloc] initWithKey:savedToken secret:savedTokenSecret];
OAConsumer* oaConsumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];

OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:twitpicUrl consumer:oaConsumer
                                    token:oaToken realm:realm signatureProvider:nil] autorelease];
[request prepare];
[request setHTTPMethod:@"POST"];
[request setValue:@"https://api.twitter.com/1/account/verify_credentials.json" forHTTPHeaderField:@"X-Auth-Service-Provider"];

NSString* authorizeHeader = [request valueForHTTPHeaderField:@"Authorization"];
[request setValue:authorizeHeader forHTTPHeaderField:@"X-Verify-Credentials-Authorization"];

如果我打印出我的标头,这就是我得到的(不包括未使用的授权标头):

    "X-Auth-Service-Provider" = "https://api.twitter.com/1/account/verify_credentials.json";
    "X-Verify-Credentials-Authorization" = "OAuth realm=\"http%3A%2F%2Fapi.twitter.com%2F\", oauth_consumer_key=\"JOvwW7mtZUjRXZRInkQI7w\", oauth_token=\"293217559-pD0HL0oE6TZSkU35mPnc7kia325oPDgMfQMTVArK\", oauth_signature_method=\"HMAC-SHA1\", oauth_signature=\"ctmCK35JFwx8qs8lQj0AYB6sUr4%3D\", oauth_timestamp=\"1304580843\", oauth_nonce=\"7EBE3EB0-641A-40EA-A57C-8D071B5E647F\", oauth_version=\"1.0\"";

我收到的错误是:

Error: The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
{"errors":[{"code":401,"message":"Could not authenticate you (header rejected by twitter)."}]}

我在这里排除了一些细节,特别是附加了帖子数据,主要是因为我认为它与错误无关。如果您不这么认为,请告诉我,我会更新问题。

有人可以帮忙吗?

更新:这是我现在在标头中成功使用的代码

#define kVerifyCredentialsUrl @"https://api.twitter.com/1/account/verify_credentials.json"
#define kTwitPicUrl @"http://api.twitpic.com/2/upload.json"

实现:

//prepare the verify credentials header
NSURL* serviceUrl = [NSURL URLWithString:kVerifyCredentialsUrl];
NSString* realm = @"http://api.twitter.com/";
OAToken* token = [[OAToken alloc] initWithKey:tokenKey secret:tokenSecret];
OAConsumer* consumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:serviceUrl consumer:consumer token:token realm:realm signatureProvider:nil];
[request setHTTPMethod:@"GET"];
[request prepare];
NSDictionary* headerDictionary = [request allHTTPHeaderFields];
NSString* oAuthHeader = [NSString stringWithString:[headerDictionary valueForKey:@"Authorization"]];
[request release];
request = nil;

//prepare the full request
serviceUrl = [NSURL URLWithString:kTwitPicUrl];
request = [[OAMutableURLRequest alloc] initWithURL:serviceUrl consumer:consumer token:token realm:realm signatureProvider:nil];
[request setHTTPMethod:@"POST"];
[request setValue:kVerifyCredentialsUrl forHTTPHeaderField:@"X-Auth-Service-Provider"];
[request setValue:oAuthHeader forHTTPHeaderField:@"X-Verify-Credentials-Authorization"];

//add the content and start the request
UIImage* imageToUpload = [UIImage imageNamed:@"test.png"];
NSData *data = UIImagePNGRepresentation(imageToUpload);
ASIFormDataRequest *asiRequest = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:kTwitPicUrl]] autorelease];
[asiRequest addRequestHeader:@"X-Auth-Service-Provider" value:kVerifyCredentialsUrl];
[asiRequest addRequestHeader:@"X-Verify-Credentials-Authorization" value:oAuthHeader];
[asiRequest setPostValue:@"Message here" forKey:@"message"];
[asiRequest setPostValue:kTwitPicAPIKey forKey:@"key"];
[asiRequest setData:data forKey:@"media"];
[asiRequest setDidFinishSelector:@selector(requestDone:)];
[asiRequest setDidFailSelector:@selector(requestWentWrong:)];
[asiRequest setDelegate:self];
[asiRequest startAsynchronous];

I'm trying to post images to TwitPic using OAuthConsumer. I keep receiving a 401 "Could not authenticate you (header rejected by twitter)." error.

I am also making use of the Twitter+OAuth library to handle the login and posting regular tweets, and then saving the token and token secret for using with TwitPic.

This is the code I am using the construct the request:

NSURL *twitpicUrl = [NSURL URLWithString:@"http://api.twitpic.com/2/upload.json"];
NSString* realm = @"http://api.twitter.com/";
OAToken* oaToken = [[OAToken alloc] initWithKey:savedToken secret:savedTokenSecret];
OAConsumer* oaConsumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];

OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:twitpicUrl consumer:oaConsumer
                                    token:oaToken realm:realm signatureProvider:nil] autorelease];
[request prepare];
[request setHTTPMethod:@"POST"];
[request setValue:@"https://api.twitter.com/1/account/verify_credentials.json" forHTTPHeaderField:@"X-Auth-Service-Provider"];

NSString* authorizeHeader = [request valueForHTTPHeaderField:@"Authorization"];
[request setValue:authorizeHeader forHTTPHeaderField:@"X-Verify-Credentials-Authorization"];

And if I print out my headers, this is what I get (excluding the unused Authorization header):

    "X-Auth-Service-Provider" = "https://api.twitter.com/1/account/verify_credentials.json";
    "X-Verify-Credentials-Authorization" = "OAuth realm=\"http%3A%2F%2Fapi.twitter.com%2F\", oauth_consumer_key=\"JOvwW7mtZUjRXZRInkQI7w\", oauth_token=\"293217559-pD0HL0oE6TZSkU35mPnc7kia325oPDgMfQMTVArK\", oauth_signature_method=\"HMAC-SHA1\", oauth_signature=\"ctmCK35JFwx8qs8lQj0AYB6sUr4%3D\", oauth_timestamp=\"1304580843\", oauth_nonce=\"7EBE3EB0-641A-40EA-A57C-8D071B5E647F\", oauth_version=\"1.0\"";

The error I receive is:

Error: The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
{"errors":[{"code":401,"message":"Could not authenticate you (header rejected by twitter)."}]}

I have excluded some detail here, specifically appending the post data, mainly because I don't think it's relevant to the error. If you think otherwise let me know and I'll update the question.

Can anyone help?

Update: Here is the code that I am now using successfully

In header:

#define kVerifyCredentialsUrl @"https://api.twitter.com/1/account/verify_credentials.json"
#define kTwitPicUrl @"http://api.twitpic.com/2/upload.json"

Implementation:

//prepare the verify credentials header
NSURL* serviceUrl = [NSURL URLWithString:kVerifyCredentialsUrl];
NSString* realm = @"http://api.twitter.com/";
OAToken* token = [[OAToken alloc] initWithKey:tokenKey secret:tokenSecret];
OAConsumer* consumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:serviceUrl consumer:consumer token:token realm:realm signatureProvider:nil];
[request setHTTPMethod:@"GET"];
[request prepare];
NSDictionary* headerDictionary = [request allHTTPHeaderFields];
NSString* oAuthHeader = [NSString stringWithString:[headerDictionary valueForKey:@"Authorization"]];
[request release];
request = nil;

//prepare the full request
serviceUrl = [NSURL URLWithString:kTwitPicUrl];
request = [[OAMutableURLRequest alloc] initWithURL:serviceUrl consumer:consumer token:token realm:realm signatureProvider:nil];
[request setHTTPMethod:@"POST"];
[request setValue:kVerifyCredentialsUrl forHTTPHeaderField:@"X-Auth-Service-Provider"];
[request setValue:oAuthHeader forHTTPHeaderField:@"X-Verify-Credentials-Authorization"];

//add the content and start the request
UIImage* imageToUpload = [UIImage imageNamed:@"test.png"];
NSData *data = UIImagePNGRepresentation(imageToUpload);
ASIFormDataRequest *asiRequest = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:kTwitPicUrl]] autorelease];
[asiRequest addRequestHeader:@"X-Auth-Service-Provider" value:kVerifyCredentialsUrl];
[asiRequest addRequestHeader:@"X-Verify-Credentials-Authorization" value:oAuthHeader];
[asiRequest setPostValue:@"Message here" forKey:@"message"];
[asiRequest setPostValue:kTwitPicAPIKey forKey:@"key"];
[asiRequest setData:data forKey:@"media"];
[asiRequest setDidFinishSelector:@selector(requestDone:)];
[asiRequest setDidFailSelector:@selector(requestWentWrong:)];
[asiRequest setDelegate:self];
[asiRequest startAsynchronous];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-11-12 01:50:04

试试这个:

NSURL *serviceURL = [NSURL URLWithString:@"https://api.twitter.com/1/account/verify_credentials.json"];
OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:serviceURL
                                                                consumer:consumer_
                                                                   token:accessToken_
                                                                   realm:@"http://api.twitter.com/"
                                                       signatureProvider:nil];
[oRequest setHTTPMethod:@"GET"];
[oRequest prepare];
NSDictionary * headerDict = [oRequest allHTTPHeaderFields];
NSString * oauthHeader = [NSString stringWithString:[headerDict valueForKey:@"Authorization"]];
[oRequest release];

//  Prepare the POST request
oRequest = nil;
serviceURL = nil;
serviceURL = [NSURL URLWithString:@"http://api.twitpic.com/2/upload.json"];
oRequest = [[OAMutableURLRequest alloc] initWithURL:serviceURL
                                               consumer:consumer_
                                                  token:accessToken_
                                                  realm:@"http://api.twitter.com/"
                                      signatureProvider:nil];
[oRequest setHTTPMethod:@"POST"];
[oRequest setValue:@"https://api.twitter.com/1/account/verify_credentials.json" forHTTPHeaderField:@"X-Auth-Service-Provider"];
[oRequest setValue:oauthHeader forHTTPHeaderField:@"X-Verify-Credentials-Authorization"];

您还应该在方法末尾的 OMutableURLRequest.m 中进行此更改 - (void)prepare:

 NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm=\"%@\", oauth_consumer_key=\"%@\", %@oauth_signature_method=\"%@\", oauth_signature=\"%@\", oauth_timestamp=\"%@\", oauth_nonce=\"%@\", oauth_version=\"1.0\"%@",
                         [realm URLEncodedString],
                         [consumer.key URLEncodedString],
                         oauthToken,
                         [[signatureProvider name] URLEncodedString],
                         [signature URLEncodedString],
                         timestamp,
                         nonce,
                         extraParameters];
if(![self valueForHTTPHeaderField:@"X-Verify-Credentials-Authorization"])
    [self setValue:oauthHeader forHTTPHeaderField:@"Authorization"];

Try this :

NSURL *serviceURL = [NSURL URLWithString:@"https://api.twitter.com/1/account/verify_credentials.json"];
OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:serviceURL
                                                                consumer:consumer_
                                                                   token:accessToken_
                                                                   realm:@"http://api.twitter.com/"
                                                       signatureProvider:nil];
[oRequest setHTTPMethod:@"GET"];
[oRequest prepare];
NSDictionary * headerDict = [oRequest allHTTPHeaderFields];
NSString * oauthHeader = [NSString stringWithString:[headerDict valueForKey:@"Authorization"]];
[oRequest release];

//  Prepare the POST request
oRequest = nil;
serviceURL = nil;
serviceURL = [NSURL URLWithString:@"http://api.twitpic.com/2/upload.json"];
oRequest = [[OAMutableURLRequest alloc] initWithURL:serviceURL
                                               consumer:consumer_
                                                  token:accessToken_
                                                  realm:@"http://api.twitter.com/"
                                      signatureProvider:nil];
[oRequest setHTTPMethod:@"POST"];
[oRequest setValue:@"https://api.twitter.com/1/account/verify_credentials.json" forHTTPHeaderField:@"X-Auth-Service-Provider"];
[oRequest setValue:oauthHeader forHTTPHeaderField:@"X-Verify-Credentials-Authorization"];

You should also make this change in OAMutableURLRequest.m at the end of the method - (void)prepare:

 NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm=\"%@\", oauth_consumer_key=\"%@\", %@oauth_signature_method=\"%@\", oauth_signature=\"%@\", oauth_timestamp=\"%@\", oauth_nonce=\"%@\", oauth_version=\"1.0\"%@",
                         [realm URLEncodedString],
                         [consumer.key URLEncodedString],
                         oauthToken,
                         [[signatureProvider name] URLEncodedString],
                         [signature URLEncodedString],
                         timestamp,
                         nonce,
                         extraParameters];
if(![self valueForHTTPHeaderField:@"X-Verify-Credentials-Authorization"])
    [self setValue:oauthHeader forHTTPHeaderField:@"Authorization"];
你如我软肋 2024-11-12 01:50:04

现在推特也可能有问题。
我的代码像您一样使用 OMutableURLRequest,多年来一直运行良好,今天我不断收到 401 错误代码。搜索twitter你会发现最近有更多人遇到这个问题。

您现在可以在 api 状态页面中看到以下内容:

我们目前正在经历升高
错误率。可能会出现间歇性的情况
加载 twitter.com 和 Twitter 时出现问题
客户。我们意识到这个问题
并采取行动。谢谢你的
耐心!

Twitter API 状态

It may also be a problem with twitter right now.
My code using OAMutableURLRequest like yours, has been working fine for ages, and today I keep getting the 401 error code. Searching twitter you will find more people having this problem recently.

This is what you can read now in the api status page:

We are currently experiencing elevated
error rates. There may be intermittent
issues loading twitter.com and Twitter
clients. We are aware of the problem
and taking action. Thanks for your
patience!

Twitter API Status

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文