2017-01-13 6 views
2

Ich möchte mit Google Drive in meiner iOS-App integrieren.iOS Google Drive-Integration

Ich habe den Code für die Autorisierung gemacht und ich bekomme den accessToken zurück, also möchte ich wissen - wohin ich von dort in Bezug auf das Abrufen der PDF-Dateien von Google Drive komme.

Mein Login-Code:

- (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]; 

             } 
      }];}];} 
+0

Möglicherweise möchten Sie [Herunterladen von Google-Dokumenten] (https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents) aktivieren. Das Beispiel zeigt, wie Sie ein Google-Dokument mit den Client-Bibliotheken im PDF-Format herunterladen können. Sie können auch in der Tabelle der unterstützten MIME-Exporttypen nachsehen, um den entsprechenden MIME-Typ für jedes Google Doc-Format zu erhalten. Weitere Informationen erhalten Sie in der [vollständigen iOS-Dokumentation] (https://developers.google.com/drive/ios/). – Teyam

+0

@ Sipho Koza, welche URL muss als redirectURI gesetzt werden? Ich stecke hier fest, muss es auch zur Entwicklerkonsole hinzugefügt werden? Bitte helfen Sie. –

Antwort

1

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

Methode zur Durchführung von Google Drive Service-Anfragen.

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

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

    // Turn on the library's 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; 
} 

Nach Google-Authentifizierung, Autorisieren driveService diese Zeilen mit:

In Ihrem Fall

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

Dann können Sie unter Methode verwenden, um Dateien von Google Drive zu holen:

- (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's 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; 

    }]; 
} 

Versuchen Sie DriveSample aus der Referenzbibliothek, fügen Sie GTLRDrive-Dateien in Ihr Projekt ein und Sie sind wieder da ady die obige Methode zu verwenden.

Um die GTMAppAuthFetcherAuthorization zu aktivieren, müssen Sie den Pod "GTMAppAuth" einschließen oder die Dateien manuell in Ihr Projekt einfügen.

Die obigen Methoden werden aus dem DriveSample-Beispiel der referenzierten Bibliothek kopiert. Dieses Beispiel funktioniert für Drive-Anforderungen.

+1

Vielen Dank für die Antwort. Das hat perfekt für mich gearbeitet. –

+0

Froh, dass es geholfen hat! –

+0

@GauravSingla Wissen Sie, wie Sie bestimmte Ordner ID erhalten? und wie man Bilder zum Laufwerk hochlädt? –