我正在尝试解决从 Instapaper 获取访问令牌的 OAuth 问题,它在他们的网站上说他们没有请求令牌/授权工作流程,他们说这与 twitter 所做的非常相似,所以这里是我做了什么:
- (void)getXAuthAccessToken
{
OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:CONSUMER_KEY secret:CONSUMER_SECRET] autorelease];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:INSTAPAPER_CLIENT_OAUTH_URL]
consumer:consumer
token:nil // xAuth needs no request token?
realm:nil // our service provider doesn't specify a realm
signatureProvider:nil]; // use the default method, HMAC-SHA1
[request setHTTPMethod:@"POST"];
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"x_auth_mode" value:@"client_auth"],
[OARequestParameter requestParameterWithName:@"x_auth_username" value:self.username],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:self.password],
nil]];
[request prepare];
AFHTTPRequestOperation *requestOAuth = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[requestOAuth setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"RESPONSE OBJECT IS %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"ERROR IS %@", error);
}];
[requestOAuth start];
}
知道为什么这不断给我一个 401 吗?
事实证明,我必须先根据此请求致电准备。但是我不能像现在看到的那样读取 responseData 只是二进制文件..我怎样才能将它转换为人类可读的?
编辑:
设置参数将参数放在标题中:
- (void)setParameters:(NSArray *)parameters
{
NSMutableString *encodedParameterPairs = [NSMutableString stringWithCapacity:256];
int position = 1;
for (OARequestParameter *requestParameter in parameters)
{
[encodedParameterPairs appendString:[requestParameter URLEncodedNameValuePair]];
if (position < [parameters count])
[encodedParameterPairs appendString:@"&"];
position++;
}
if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
[self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", [[self URL] URLStringWithoutQuery], encodedParameterPairs]]];
else
{
// POST, PUT
NSData *postData = [encodedParameterPairs dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[self setHTTPBody:postData];
[self setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
}