gpt4 book ai didi

ios - AFNetworking http 基本身份验证给出错误 "Your API key was not formatted properly"

转载 作者:行者123 更新时间:2023-12-01 18:44:56 31 4
gpt4 key购买 nike

尝试使用 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"];

关于ios - AFNetworking http 基本身份验证给出错误 "Your API key was not formatted properly",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36961734/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com