ios - Youtube API iOS : Request failed: bad request (400)
<p><p>我正在开发一个需要获取 youtubechannel 并订阅它的 iOS 应用程序。
我可以获得没有 OAuth2 的 channel 列表,但要订阅它,我需要通过 OAuth2 标准获取访问 token 。 </p>
<p>我可以通过一些手工获得访问 token 。但是,当我将此 token 发送到 header 并请求正文进行订阅时,它会给我“错误请求(400)”错误。</p>
<h2>发送请求代码</h2>
<pre><code>NSString *URLString = "];
AFHTTPRequestOperationManager *manager = ;
AFHTTPRequestSerializer *requestSerializer = ;
] forHTTPHeaderField:@"Authorization"];
;
manager.requestSerializer = requestSerializer;
NSDictionary *parameters = @{@"snippet":@{
@"resourceId":@{
@"channelId":[[ 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");
}];
}
</code></pre>
<h2>出现跟随错误</h2>
<pre><code>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";
} },
</code></pre>
<p>如有任何帮助,将不胜感激。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我发现了问题。有两个错误。</p>
<ol>
<li><p>其实我是在设置不同的requestSerializer,传递不同的序列化器。</p></li>
<li><p>我正在将 APIKey 传递给 url 进行订阅,但不需要传递它。</p></li>
</ol>
<p><strong>我的新代码是</strong></p>
<pre><code> NSString *URLString = ;
AFHTTPRequestOperationManager *manager = ;
AFHTTPRequestSerializer *requestSerializer = ;
] forHTTPHeaderField:@"Authorization"];
;
manager.requestSerializer = requestSerializer;
NSDictionary *parameters =@{@"snippet":@{
@"resourceId":@{
@"channelId":[[ 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");
}];
</code></pre></p>
<p style="font-size: 20px;">关于ios - Youtube API iOS : Request failed: bad request (400),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37433203/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37433203/
</a>
</p>
页:
[1]