【问题标题】:iOS Google Drive integrationiOS Google Drive 集成
【发布时间】:2017-01-13 08:39:57
【问题描述】:

我想在我的 iOS 应用中集成 Google Drive。

我已经完成了授权代码,并且我正在取回 accessToken,所以我想知道 - 从 Google Drive 中检索 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];

                                        }
            }];}];}

【问题讨论】:

  • 您可能需要检查Downloading Google Documents。给定的示例演示了如何使用客户端库下载 PDF 格式的 Google 文档。您还可以查看支持的导出 MIME 类型表以获取每种 Google Doc 格式的相应 MIME 类型。如需更详细的信息,您可能需要查看full iOS documentation
  • @Sipho Koza,什么 url 需要设置为 redirectURI?我被困在这里,它是否也需要添加到开发者控制台?请帮忙。
  • 我写了一个中型博客,一步一步解释。看看这个medium.com/@kunalgupta1508/…

标签: objective-c google-drive-api


【解决方案1】:

代码参考库:https://github.com/google/google-api-objectivec-client-for-rest

执行 Google Drive Services 请求的方法。

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

在 Google 身份验证之后,使用这些行授权 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 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'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 文件,您就可以使用上述方法了。

要使 GTMAppAuthFetcherAuthorization 正常工作,您必须在项目中包含 pod“GTMAppAuth”或手动包含文件。

实际上,上述方法是从引用库的 DriveSample 示例中复制的,此示例适用于 Drive 请求。

【讨论】:

  • 感谢您的回复。这对我来说很完美。
  • @GauravSingla 你知道如何获取特定文件夹的 id 吗?以及如何将图像上传到驱动器?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 2012-07-04
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多