我正在尝试使用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];
答案 0 :(得分:0)
试试这个:
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"];
您还应该在方法结束时在OAMutableURLRequest.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"];
答案 1 :(得分:0)
现在推特也可能存在问题。 我的代码使用像你这样的OAMutableURLRequest,已经很好地工作了很长时间,今天我一直得到401错误代码。搜索推特,你会发现最近有更多人遇到这个问题。
这是您现在可以在api状态页面中阅读的内容:
我们目前正在经历升高 错误率。可能会有间歇性的 加载twitter.com和Twitter的问题 客户端。我们意识到了这个问题 并采取行动。谢谢你的 耐心!