OStack程序员社区-中国程序员成长平台

标题: ios - Youtube API iOS : Request failed: bad request (400) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:43
标题: ios - Youtube API iOS : Request failed: bad request (400)

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

我可以通过一些手工获得访问 token 。但是,当我将此 token 发送到 header 并请求正文进行订阅时,它会给我“错误请求(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";
} },

如有任何帮助,将不胜感激。



Best Answer-推荐答案


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

  1. 其实我是在设置不同的requestSerializer,传递不同的序列化器。

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

我的新代码是

 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");
        }];

关于ios - Youtube API iOS : Request failed: bad request (400),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433203/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4