Google Drive Integration iOS

I want to integrate with Google Drive in my iOS app.

I made authorization code and I am returning accessToken, so I want to know where to go to find PDF files from Google Drive.

My login:

- (IBAction)signInButtonTapped:(id)sender {    
    NSURL *issuer = [NSURL URLWithString:kIssuer];
    NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];

    [self logMessage:@"Fetching configuration for issuer: %@", issuer];
    // discovers endpoints

    [OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
                                                        completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {


               if (!configuration) {
                    [self logMessage:@"Error retrieving discovery document: %@", [error localizedDescription]];
                    [self setAuthState:nil];
                    return;
                }

                [self logMessage:@"Got configuration: %@", configuration];

                                                        // builds authentication request
                OIDAuthorizationRequest *request =
                [[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
                                                                                                      clientId:kClientID
                                                                                                        scopes:@[OIDScopeOpenID, OIDScopeProfile]
                                                                                                   redirectURL:redirectURI
                                                                                                  responseType:OIDResponseTypeCode
                                                                                          additionalParameters:nil];
                                                        // performs authentication request
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [self logMessage:@"Initiating authorization request with scope: %@", request.scope];

            appDelegate.currentAuthorizationFlow =
            [OIDAuthState authStateByPresentingAuthorizationRequest:request
                                                    presentingViewController:self
                                                                            callback:^(OIDAuthState *_Nullable authState,
                                                                                                                  NSError *_Nullable error) {
                                        if (authState) {

                                            [self setAuthState:authState];

                                            [self logMessage:@"Got authorization tokens. Access token: %@", authState.lastTokenResponse.accessToken];
                                            [self logMessage:@"Got authorization tokens. Refresh Access token %@", authState.refreshToken];



                                            } else {

                                            [self logMessage:@"Authorization error: %@", [error localizedDescription]];

                                            [self setAuthState:nil];

                                        }
            }];}];}
+2
source share
2 answers

Code Reference Library: https://github.com/google/google-api-objectivec-client-for-rest

A method for querying Google Drive services.

- (GTLRDriveService *)driveService {
    static GTLRDriveService *service;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    service = [[GTLRDriveService alloc] init];

    // Turn on the library shouldFetchNextPages feature to ensure that all items
    // are fetched.  This applies to queries which return an object derived from
    // GTLRCollectionObject.
    service.shouldFetchNextPages = YES;

    // Have the service object set tickets to retry temporary error conditions
    // automatically
    service.retryEnabled = YES;
    });
    return service;
}

After Google Authentication, authorize driveService using the following lines:

In your case

if (authState) {
    // Creates a GTMAppAuthFetcherAuthorization object for authorizing requests.
                GTMAppAuthFetcherAuthorization *gtmAuthorization =
                [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];

                // Sets the authorizer on the GTLRYouTubeService object so API calls will be authenticated.
                strongSelf.driveService.authorizer = gtmAuthorization;

                // Serializes authorization to keychain in GTMAppAuth format.
                [GTMAppAuthFetcherAuthorization saveAuthorization:gtmAuthorization
                                                toKeychainForName:kKeychainItemName];

    // Your further code goes here
    //
    [self fetchFileList];
}

Then you can use the method below to extract files from Google Drive:

- (void)fetchFileList {

  __block GTLRDrive_FileList *fileListNew = nil;

  GTLRDriveService *service = self.driveService;

  GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];

  // Because GTLRDrive_FileList is derived from GTLCollectionObject and the service
  // property shouldFetchNextPages is enabled, this may do multiple fetches to
  // retrieve all items in the file list.

  // Google APIs typically allow the fields returned to be limited by the "fields" property.
  // The Drive API uses the "fields" property differently by not sending most of the requested
  // resource fields unless they are explicitly specified.
  query.fields = @"kind,nextPageToken,files(mimeType,id,kind,name,webViewLink,thumbnailLink,trashed)";

  GTLRServiceTicket *fileListTicket;

  fileListTicket = [service executeQuery:query
                    completionHandler:^(GTLRServiceTicket *callbackTicket,
                                        GTLRDrive_FileList *fileList,
                                        NSError *callbackError) {
    // Callback
    fileListNew = fileList;

  }];
}

Try DriveSample from the help library, include the GTLRDrive files in your project, and you are ready to use the method described above.

GTMAppAuthFetcherAuthorization, "GTMAppAuth" .

DriveSample , .

+1

401 .

0

All Articles