我在iPhone应用中使用Youtube API v3.0,并且我已经使用Google plus API处理了YouTube授权。现在我需要按照链接中的示例订阅youtube频道..
https://developers.google.com/youtube/v3/docs/subscriptions/insert#examples
所以在我的代码中..
NSString *URLString = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=%@", API_KEY];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:[AOTube accessToken] forHTTPHeaderField:@"Authorization"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer = requestSerializer;
NSDictionary *parameters = @{@"snippet":@{
@"resourceId":@{
@"channelId":CHANNEL_ID,
@"kind":@"youtube#channel"
}
}
};
[manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
我从失败块中收到以下错误..
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x7f8983d5cab0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f8983d40ca0> { URL: https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=AIzaSyB1mipmXC8dmXduIJXJ7qt9CxmLlnuc8aw } { status code: 401, headers {
"Cache-Control" = "private, max-age=0";
"Content-Encoding" = gzip;
"Content-Length" = 162;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Sun, 04 Jan 2015 22:00:55 GMT";
Expires = "Sun, 04 Jan 2015 22:00:55 GMT";
Server = GSE;
Vary = "Origin, X-Origin";
"Www-Authenticate" = "Bearer realm=\"https://accounts.google.com/AuthSubRequest\"";
"alternate-protocol" = "443:quic,p=0.02";
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";
} }, NSErrorFailingURLKey=https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&key=ewrqDSyB1sfrgdmXduIJfwrgrwgLlnuc8aw, com.alamofire.serialization.response.error.data=<7b0a2022 6572726f 72223a20 7b0a2020 22657272 6f727322 3a205b0a 2020207b 0a202020 2022646f 6d61696e 223a2022 676c6f62 616c222c 0a202020 20227265 61736f6e 223a2022 61757468 4572726f 72222c0a 20202020 226d6573 73616765 223a2022 496e7661 6c696420 43726564 656e7469 616c7322 2c0a2020 2020226c 6f636174 696f6e54 79706522 3a202268 65616465 72222c0a 20202020 226c6f63 6174696f 6e223a20 22417574 686f7269 7a617469 6f6e220a 2020207d 0a20205d 2c0a2020 22636f64 65223a20 3430312c 0a202022 6d657373 61676522 3a202249 6e76616c 69642043 72656465 6e746961 6c73220a 207d0a7d 0a>, NSLocalizedDescription=Request failed: unauthorized (401)}
我确定API_KEY,CHANNEL_ID和访问令牌没有任何问题,因为我已经成功地在其他网络服务中使用它们
答案 0 :(得分:0)
您没有正确设置授权标头字段。
而不是:
[requestSerializer setValue:[AOTube accessToken] forHTTPHeaderField:@"Authorization"];
使用:
NSString *authValue = [NSString stringWithFormat:@"Bearer %@", [AOTube accessToken]];
[requestSerializer setValue: authValue forHTTPHeaderField:@"Authorization"];
接下来将您的请求序列化程序更正为:
AFHTTPRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
在授权过程中,添加以下至少一个范围:https://www.googleapis.com/auth/youtubepartner或https://www.googleapis.com/auth/youtube
样品:
signIn.scopes = @[ kGTLAuthScopePlusLogin, @"https://www.googleapis.com/auth/youtube" ];
如果使用Google API正确签名,则不必在URL中包含API密钥请求参数。