Creating an Authorized Google API for iOS

I have enabled Google Sign-in on my iOS app and retrieved the access tokens upon sign-in. Now I want to make authorized API calls to Google, but I'm not sure how to do this to enable the token. Can someone please share some code that I can use to enable this?
Thanks a lot,
Luke

+3


source to share


2 answers


After you've logged in and purchased tokens, you create service instances and then attach an "authorizer". The Google Objective-C client supports multiple services: https://code.google.com/p/google-api-objectivec-client/

Here's an example using Google +:

Obj-C (with ARC support)

GTLServicePlus* plusService = [[GTLServicePlus alloc] init];
plusService.retryEnabled = YES;

# set an authorizer with your tokens
[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];

# submit authenticated queries, assuming your scopes & tokens are legit
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleListWithUserId:@"me"
                                     collection:kGTLPlusCollectionVisible];


[plusService executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket,
                            GTLPlusPeopleFeed *peopleFeed,
                            NSError *error) {
               //  ... your callback ...
       }];

      



Swift

var plusService = GTLServicePlus()
plusService.retryEnabled = true

# set an authorizer with your tokens
plusService.authorizer = GPPSignIn.sharedInstance().authentication

if let plusQuery = GTLQueryPlus.queryForPeopleListWithUserId("me",
                collection: kGTLPlusCollectionVisible) as? GTLPlusQuery {
    // execute the query
    plusService.executeQuery(plusQuery) { (ticket: GTLServiceTicket!,
                                           peopleFeed: GTLPlusPeopleFeed!,
                                           error: NSError!) -> Void in
        // ... your callback ...
    }
}

      

There is an example using the Google Obj-C API from YouTube. Mark the line 229 in YouTubeSampleWindowController.m

to customize your object GTLServiceYouTube

and line 261 for an example of using it with an object GTLQueryYouTube

.

There are also some nice CocoaDocs . This method is probably what you need.

+2


source


This was resolved at the end with BAHYouTubeOAuth

, available here .
I spoke with the creator of this and he said that the changes are in effect regarding better token handling.
If it has not been officially changed, see my amendments here



0


source







All Articles