我正在使用 YouTube DATA API 为 iOS 开发应用程序。 Official google documentation有链接到this SDK .但它是 SDK 不工作,因为找不到 pod“gtm-oauth2”的关系“gtm-http-fetcher”之一。不仅如此,自述文件还说:
NOTE: This library has been replaced by Google APIs Client Library for Objective-C For REST. New applications should use that instead; existing application should be updated as soon as possible as the Google servers will eventually stop accepting JSON-RPC requests.
好的,我安装下一个 pod:
pod 'GoogleAPIClientForREST/YouTube'
pod 'GoogleAPIClientForREST/Oauth2'
pod "youtube-ios-player-helper"
例如,我尝试获取我的视频列表:
self.service = [GTLRYouTubeService new];
self.service.APIKey = @"API_KEY";
GTLRYouTubeQuery_ActivitiesList *query = [GTLRYouTubeQuery_ActivitiesList queryWithPart:@"contentDetails"];
query.mine = YES;
[self.service executeQuery:query completionHandler:^(GTLRServiceTicket *ticket, id object, NSError *error) {
NSLog(@"object is %@",object);
NSLog(@"error is %@",error);
}];
并上传视频:
self.service = [GTLRYouTubeService new];
self.service.APIKey = @"API_KEY";
GTLRYouTube_Activity *activObjec = [GTLRYouTube_Activity new];
GTLRYouTubeQuery_ActivitiesInsert *query = [GTLRYouTubeQuery_ActivitiesInsert queryWithObject:activObjec part:@"fileDetails"];
[self.service executeQuery:query completionHandler:^(GTLRServiceTicket *ticket, id object, NSError *error) {
NSLog(@"object is %@",object);
NSLog(@"error is %@",error);
}];
我收到上传视频的 401 错误“需要登录”和 401 错误“请求使用了 mine
参数但未获得正确授权。”。
其中我在 iOS 的谷歌开发者控制台 API key (没有 bundle id)和 OAuth 2.0 标识符中生成:
然后我添加到信息 plist 文件:
<key>CLIENT_ID</key>
<string>client id from google console</string>
<key>REVERSED_CLIENT_ID</key>
<string>reversed client id from google console</string>
<key>PLIST_VERSION</key>
<string>1</string>
<key>BUNDLE_ID</key>
<string>com.ncl.VideoImploder</string>
但我不明白,这是错误的。我做错了什么?
我看到方法: - (void)setAuthorizer:(id )授权; 在 GTLRYouTubeService 中,但我不明白向它发送 client_id 和 api_key 的方法,我在 GTMFetcherAuthorizationProtocol 协议(protocol)中没有看到类似的属性。
请告诉我,如何在 GoogleAPIClientForREST 中设置授权?
最佳答案
安装以下 pod 并使用它登录:
pod 'Google/SignIn'
使用 GIDSignIn
登录后创建 GTLRYouTubeService
实例:
let service = GTLRYouTubeService()
service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
所有带有服务
的请求都会被授权
关于ios - 我如何通过适用于 iOS 的 GoogleAPIClientForREST 授权和上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38521349/