【问题标题】:AFHTTPSessionManager setting authorization headerAFHTTPSessionManager 设置授权头
【发布时间】:2017-01-25 11:01:37
【问题描述】:

我已经被这个问题困扰了一段时间,我正在使用 Youtube 的 api,但我一直收到 403 错误。我正在使用负责 OAuth2 的 Google 登录

 NSString *authToken = [[NSString alloc]initWithFormat:@"Bearer %@",[Lockbox unarchiveObjectForKey:@"accessToken"]];

 NSDictionary *parameters = @{@"mine":@"true", @"part": @"contentDetails"};
 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

 manager.requestSerializer = [AFJSONRequestSerializer serializer];
 [manager.requestSerializer setValue:authToken forHTTPHeaderField:@"Authorization"];
NSLog(@"%@----------", authToken); //Prints correct value

 [manager GET:@"https://www.googleapis.com/youtube/v3/channels" parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

我知道 GET url 是正确的,因为我已经尝试在 OAuth 2.0 Playground 上使用它并且得到 200 响应。

以下是我返回的 403 错误响应

{ status code: 403, headers {
"Cache-Control" = "private, max-age=0";
"Content-Encoding" = gzip;
"Content-Length" = 356;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Sat, 17 Sep 2016 08:18:16 GMT";
Expires = "Sat, 17 Sep 2016 08:18:16 GMT";
Server = GSE;
Vary = "Origin, X-Origin";
"alt-svc" = "quic=\":443\"; ma=2592000; v=\"36,35,34,33,32\"";
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";

} }

我没有在标题中看到 Authorization 字段,但我相信我设置正确并以正确的方式查看 SO。是否有任何明显的问题,因为我已经被困了好几个小时了

【问题讨论】:

  • 这可能会有所帮助:github.com/AFNetworking/AFOAuth2Manager
  • K -_-,我以为我在 Google 上启用了 Youtube API 3,但我没有,这就是我获得 403 禁止访问的原因。所以上面的代码是正确的,只是我必须启用 Youtube API。再见,我的生活结束了

标签: ios objective-c youtube-api


【解决方案1】:
NSString *string = [NSString stringWithFormat:@"https://www.google.com/m8/feeds/contacts/default/full/%@?alt=json",contactID];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"DELETE" URLString:string parameters:nil error:nil];
NSString *token = [NSString stringWithFormat:@"Bearer %@",[[GoogleContactManager sharedManager] token]]; //bearer is my personal requirement your's can differ

[req setValue:token forHTTPHeaderField:@"Authorization"];
[req setValue:@"3.0" forHTTPHeaderField:@"GData-Version"];
[req setValue:@"*" forHTTPHeaderField:@"if-match"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2013-01-15
    • 2015-12-02
    • 2014-07-11
    相关资源
    最近更新 更多