Can I use google drive sdk with authentication info from google sign-in sdk on iOS?

前端 未结 2 1244
感动是毒
感动是毒 2021-01-06 08:32

We already have a login module uses Google Sign-In sdk. Google Sign-In gives a GIDAuthentication object after login succeed.

Now I want to access user\'s google dri

2条回答
  •  渐次进展
    2021-01-06 09:20

    / Creates the auth controller for authorizing access to Google Drive.
    - (GTMOAuth2ViewControllerTouch *)createAuthController
    {
        GTMOAuth2ViewControllerTouch *authController;
        authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:@"https://www.googleapis.com/auth/drive"
                                                                    clientID:kClientID
                                                                clientSecret:kClientSecret
                                                            keychainItemName:kKeychainItemName
                                                                    delegate:self
                                                            finishedSelector:@selector(viewController:finishedWithAuth:error:)];
        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;
        }
        else
        {
            self.driveService.authorizer = authResult;
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }
    }-(void)loadDriveFiles
    {
        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];
            }
            else
            {
                NSLog(@"An error occurred: %@", error);
            }
        }];
    }
    

提交回复
热议问题