2017-01-13 26 views
2

iOSアプリでGoogleドライブと統合したい。iOS Googleドライブの統合

私は認証のためのコードを実行しましたが、私はaccessTokenを取り戻しています。そのため、GoogleドライブからPDFファイルを取得するための場所を教えてください。

マイログインコード:

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

[Googleドキュメントのダウンロード](https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents)にチェックを入れたい場合があります。ここでは、クライアントライブラリを使用してGoogleドキュメントをPDF形式でダウンロードする方法を示します。サポートされているエクスポートMIMEタイプの表を調べて、すべてのGoogleドキュメントフォーマットの対応するMIMEタイプを取得することもできます。 詳細については、[完全なiOSドキュメント](https://developers.google.com/drive/ios/)をご確認ください。 – Teyam

+0

@Sipho Koza、どのURLをredirectURIとして設定する必要がありますか?私はここで立ち往生しています、開発者コンソールにも追加する必要がありますか?助けてください。 –

答えて

1

コードリファレンスライブラリ:Googleドライブサービスリクエストを実行するためのhttps://github.com/google/google-api-objectivec-client-for-rest

方法。 Googleの認証後

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

、承認driveServiceこれらの行使用:次に、あなたの場合は

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

を、Googleドライブからファイルを取得するための方法の下に使用することができます:

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

    }]; 
} 

参照ライブラリからDriveSampleを試し、プロジェクトにGTLRDriveファイルを追加してください上記の方法を使用するady。

GTMAppAuthFetcherAuthorizationを機能させるには、ポッド "GTMAppAuth"を含めるか、プロジェクトに手動でファイルを含める必要があります。

実際には、上記のメソッドは参照されたライブラリのDriveSampleの例からコピーされ、この例はDrive要求に対して正常に動作しています。

+1

レスポンスありがとうございます。これは私にとって完璧に機能しました。 –

+0

うれしかった! –

+0

@ GauravSingla特定のフォルダのIDを取得する方法を知っていますか?ドライブに画像をアップロードする方法は? –

関連する問題