OAuthConsumer 和 Twitpic
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
您还应该在方法末尾的 OMutableURLRequest.m 中进行此更改 - (void)prepare:
Try this :
You should also make this change in OAMutableURLRequest.m at the end of the method - (void)prepare:
现在推特也可能有问题。
我的代码像您一样使用 OMutableURLRequest,多年来一直运行良好,今天我不断收到 401 错误代码。搜索twitter你会发现最近有更多人遇到这个问题。
您现在可以在 api 状态页面中看到以下内容:
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:
Twitter API Status