ios - 我如何通过适用于 iOS 的 GoogleAPIClientForREST 授权和上传视频

标签 ios objective-c google-oauth youtube-data-api google-api-client

我正在使用 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 标识符中生成: enter image description here enter image description here

然后我添加到信息 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/

相关文章:

ios - Swift MKMapView Pin 注释问题

iphone - 从深度嵌套的 UIControl 向 View Controller 发送消息的最佳方式?

ios - 使用 CCTMXTiledMap 迭代 cocos2d 中所有可用的图 block

oauth-2.0 - 谷歌oauth错误invalid_client没有支持电子邮件

javascript - 从 Chrome 扩展程序访问 GMail API? 403 禁忌

ios - 使用 Apple ID 登录

html - 与所有屏幕尺寸的小屏幕相同的顶部栏

objective-c - iOS8上UIActivityViewController的 "More"部分下的UIBarButtonItem如何设置颜色?

objective-c - 打乱 NSString 顺序的快速方法?

c# - Google Drive Api - 带有 Entity Framework 的自定义 IDataStore