Search code examples
iosobjective-cgoogle-oauthyoutube-data-apigoogle-api-client

How I can authorize and upload video via GoogleAPIClientForREST for iOS


I doing application for iOS with YouTube DATA API. Official google documentation have link to this SDK. But it is SDK dont work, because one of relationship "gtm-http-fetcher" for pod "gtm-oauth2" not found. More than that, ReadMe say:

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.

Ok, I install next pods:

pod 'GoogleAPIClientForREST/YouTube'
pod 'GoogleAPIClientForREST/Oauth2'
pod "youtube-ios-player-helper"

For example I try get list of my videos:

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

and upload video:

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

And I get 401 error "Login Required" for upload video and 401 error "The request uses the mine parameter but is not properly authorized.".

Wherein I generate in google developer console API key for iOS (without bundle id) and OAuth 2.0 identifier: enter image description here enter image description here

and I add to info plist file:

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

But I dont understand, that goes wrong. What I doing incorrect?

I see method: - (void)setAuthorizer:(id )authorize; in GTLRYouTubeService, but I dont understand send client_id and api_key to it method, I dont see similar properties in GTMFetcherAuthorizationProtocol protocol.

Please, say me, how setup authorization in GoogleAPIClientForREST?


Solution

  • Install following pod and use it to sign in:

    pod 'Google/SignIn'
    

    After you sign in with GIDSignIn create GTLRYouTubeService instance:

    let service = GTLRYouTubeService()
    service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
    

    All requests with service will be authorized