我如何通过 GoogleAPIClientForREST 为 iOS 授权和上传视频
How I can authorize and upload video via GoogleAPIClientForREST for iOS
我正在使用 YouTube DATA API 申请 iOS。
Official google documentation have link to 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.
好的,我接下来安装pods:
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 错误 "Login Required" 和 401 错误 "The request uses the mine
parameter but is not properly authorized."。
其中我在 google 开发人员控制台 API 中为 iOS 生成密钥(没有 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 协议中看不到类似的属性。
请告诉我,如何在 GoogleAPIClientForREST 中设置授权?
安装以下 pod 并使用它登录:
pod 'Google/SignIn'
使用 GIDSignIn
登录后创建 GTLRYouTubeService
实例:
let service = GTLRYouTubeService()
service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
所有 service
的请求都将被授权
我正在使用 YouTube DATA API 申请 iOS。 Official google documentation have link to 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.
好的,我接下来安装pods:
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 错误 "Login Required" 和 401 错误 "The request uses the mine
parameter but is not properly authorized."。
其中我在 google 开发人员控制台 API 中为 iOS 生成密钥(没有 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 协议中看不到类似的属性。
请告诉我,如何在 GoogleAPIClientForREST 中设置授权?
安装以下 pod 并使用它登录:
pod 'Google/SignIn'
使用 GIDSignIn
登录后创建 GTLRYouTubeService
实例:
let service = GTLRYouTubeService()
service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
所有 service
的请求都将被授权