AFNetworking http 基本身份验证给出错误 "Your API key was not formatted properly"
AFNetworking http basic authentication gives error "Your API key was not formatted properly"
正在尝试使用 postmates api,但 运行 遇到授权问题。
引用他们的网站:
AUTHENTICATION
The Postmates API requires authentication by HTTP Basic Auth headers.
Your API key should be included as the username. The password should
be left empty.
The actual header that is used will be a base64-encoded string like
this:
Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==
我试过像这样使用 AFNetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"(my postmates key)" password:@"" persistence:NSURLCredentialPersistenceNone];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:@"https://api.postmates.com/v1/delivery_zones" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCredential:credential];
[operation setResponseSerializer:[AFJSONResponseSerializer alloc]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", error);
}];
[manager.operationQueue addOperation:operation];
我收到的错误消息是
Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.
Success: {
code = "invalid_authorization_header";
kind = error;
message = "Your API key was not formatted properly";
}
您可能想尝试以下操作,而不是创建凭据:
[request setValue:@"Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==" forHTTPHeaderField:@"Authorization"];
正在尝试使用 postmates api,但 运行 遇到授权问题。
引用他们的网站:
AUTHENTICATION
The Postmates API requires authentication by HTTP Basic Auth headers. Your API key should be included as the username. The password should be left empty.
The actual header that is used will be a base64-encoded string like this:
Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==
我试过像这样使用 AFNetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"(my postmates key)" password:@"" persistence:NSURLCredentialPersistenceNone];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:@"https://api.postmates.com/v1/delivery_zones" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCredential:credential];
[operation setResponseSerializer:[AFJSONResponseSerializer alloc]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", error);
}];
[manager.operationQueue addOperation:operation];
我收到的错误消息是
Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.
Success: {
code = "invalid_authorization_header";
kind = error;
message = "Your API key was not formatted properly";
}
您可能想尝试以下操作,而不是创建凭据:
[request setValue:@"Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==" forHTTPHeaderField:@"Authorization"];