gpt4 book ai didi

iOS Youtube API v3 订阅 youtube channel

转载 作者:行者123 更新时间:2023-12-03 06:25:08 25 4
gpt4 key购买 nike

我在 iPhone 应用程序中使用 Youtube API v3.0,并且我已经使用 Google plus API 处理了 youtube 授权。现在我需要按照链接中的示例订阅一个 youtube channel 。

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);

}];

我从故障 block 中收到以下错误..
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 和访问 token 没有任何问题,因为我已经在其他 Web 服务中成功使用它们

最佳答案

您没有正确设置授权 header 字段。

代替:

[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/youtubepartnerhttps://www.googleapis.com/auth/youtube

样本:
signIn.scopes = @[ kGTLAuthScopePlusLogin, @"https://www.googleapis.com/auth/youtube" ];

如果您使用 Google API 正确签名,则不必在 URL 中包含 API key 请求参数。

关于iOS Youtube API v3 订阅 youtube channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27770865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com