Youtube API iOS:请求失败:错误请求(400)

时间:2016-05-25 09:38:07

标签: ios youtube

我正在开发一个iOS应用程序,需要获取youtube频道并订阅它。 我可以获得没有OAuth2的频道列表但是为了订阅它我需要通过OAuth2标准获得访问令牌。

我可以通过一些手工获得访问令牌。但是当我将此令牌发送到标题中并请求进入正文订阅主体时,它会给我“错误请求(400)'错误。

发送请求代码

NSString *URLString = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=%@", @"[mykey]"];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];

    [requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",[self accessToken]] forHTTPHeaderField:@"Authorization"];
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    manager.requestSerializer = requestSerializer;

    NSDictionary *parameters = @{@"snippet":@{
                                         @"resourceId":@{
                                                 @"channelId":[[[videoCountDetailArray objectAtIndex:0] valueForKey:@"snippet"] valueForKey:@"channelId"],
                                                 @"kind":@"youtube#channel"
                                                 }
                                         }
                                 };

    [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"response ; %@",responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failed");        
    }];
    }

获取以下错误

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fcfbccae3a0> { URL: https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=myKey } { status code: 400, headers {
    "Cache-Control" = "private, max-age=0";
    "Content-Encoding" = gzip;
    "Content-Length" = 118;
    "Content-Type" = "application/json; charset=UTF-8";
    Date = "Wed, 25 May 2016 09:28:57 GMT";
    Expires = "Wed, 25 May 2016 09:28:57 GMT";
    Server = GSE;
    Vary = "Origin, X-Origin";
    "alt-svc" = "quic=\":443\"; ma=2592000; v=\"34,33,32,31,30,29,28,27,26,25\"";
    "alternate-protocol" = "443:quic";
    "x-content-type-options" = nosniff;
    "x-frame-options" = SAMEORIGIN;
    "x-xss-protection" = "1; mode=block";
} },

任何帮助都会得到满足。

1 个答案:

答案 0 :(得分:1)

我发现了问题。有两个错误。

  1. 实际上,我正在设置不同的requestSerialiser并传递不同的序列化器。

  2. 我将APIKey传递给url进行订阅但不需要传递它。

  3. 我的新代码是

     NSString *URLString = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/subscriptions?part=snippet"];
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            AFHTTPRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
            [requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",[self accessToken]] forHTTPHeaderField:@"Authorization"];
            [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
            manager.requestSerializer = requestSerializer;
    
            NSDictionary *parameters =  @{@"snippet":@{
                                                  @"resourceId":@{
                                                          @"channelId":[[[videoCountDetailArray objectAtIndex:0] valueForKey:@"snippet"] valueForKey:@"channelId"],
                                                          @"kind":@"youtube#channel"
                                                          }
                                                  }
                                          };
    
            NSLog(@"parameters : %@",parameters);
            [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"response ; %@",responseObject);
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"failed");
            }];