Can I use sdk on google drive with authentication information from google login sdk for iOS?

We already have a login module that uses login to log into google. Once logged in, Google Sign-In provides the GIDAuthentication object .

Now I want to get access to Google Google Drive using gdvdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddbdddddddddddddddddddddddddddddddddddddddddddddddddddddddbddddddddddddddbdddddddddddd Can I use GIDAuthentication

to create the disc GTMOAuth2Authentication sdk?

Manually assigning a value accessToken

does not seem to work (drive area added).


source to share

2 answers

Yes, you can!

Use the following steps:

  • Follow all the steps to add Google SignIn as described in: . Make sure you have enabled the Google Drive API for your project.

  • Remember to add the Google Drive API scope when initializing the login object:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Initialize sign-in
        var configureError: NSError?
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
        GIDSignIn.sharedInstance().delegate = self
        // Use here whatever auth scope you wish (e.g., kGTLAuthScopeDriveReadonly, 
        // kGTLAuthScopeDriveMetadata, etc..)
        // You can obviously append more scopes to allow access to more services,
        // other than Google Drive.
        return true

  • In your AppDelegate

    (or other accessible location) add:

    var myAuth: GTMFetcherAuthorizationProtocol? = nil

  • In the signIn

    delegate function (assuming it's also set to AppDelegate

    ), add the following code:

    func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
        if (error == nil) {
            // Logged into google services successfully! 
            // Save relevant details from user.authentication to refresh the token when needed.      
            // Set GTMOAuth2Authentication authoriser for your Google Drive service
            myAuth = user.authentication.fetcherAuthorizer()
        } else {
            // Error signing into Google services

  • Finally, wherever you install your GoogleServiceDrive, you can also set its authorizer by simply setting:

    let service = GTLServiceDrive()
    service.authorizer = (UIApplication.sharedApplication().delegate as! AppDelegate).myAuth

  • Now you can use google example code

    if let authorizer = service.authorizer, canAuth = authorizer.canAuthorize where canAuth {
        // service is authorised and can be used for queries
    } else {
        // service is not authorised 



/ Creates the auth controller for authorizing access to Google Drive.
- (GTMOAuth2ViewControllerTouch *)createAuthController
    GTMOAuth2ViewControllerTouch *authController;
    authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:@""
    return authController;

// Handle completion of the authorization process, and updates the Drive service
// with the new credentials.
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)authResult
                 error:(NSError *)error
    if (error != nil)
        [self showAlert:@"Authentication Error" message:error.localizedDescription];
        self.driveService.authorizer = nil;
        self.driveService.authorizer = authResult;
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    NSMutableArray *driveFiles = [[NSMutableArray alloc] init];
    NSMutableArray *downloadFiles = [[NSMutableArray alloc] init];

    GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
    query.q = [NSString stringWithFormat:@"'%@' IN parents", @"root"];
    [self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                              GTLDriveFileList *files,
                                                              NSError *error) {
        if (error == nil)
            for(id key in files.items) {
                NSString *titleStr = [key valueForKey:@"title"];
                [driveFiles addObject:titleStr];

                NSString *downloadStr = [key valueForKey:@"downloadUrl"];
                [downloadFiles addObject:downloadStr];
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            FilesViewController *filesView = (FilesViewController *)[storyboard instantiateViewControllerWithIdentifier:@"filesView"];
            [filesView initwithName:driveFiles anddownload:downloadFiles];
            [self.navigationController pushViewController:filesView animated:YES];
            NSLog(@"An error occurred: %@", error);




All Articles