ios - 我可以在 iOS 上将 google drive sdk 与来自 google 登录 sdk 的身份验证信息一起使用吗?

标签 ios google-drive-api

我们已经有一个使用 Google Sign-In sdk 的登录模块。 Google 登录给出 GIDAuthentication登录成功后的对象。

现在我想使用需要 GTMOAuth2Authentication 来提供身份验证信息的 google drive sdk 访问用户的 google drive。那么我可以使用 GIDAuthentication 为驱动器 sdk 构造一个 GTMOAuth2Authentication 吗?

手动分配 accessToken 值似乎不起作用(已添加驱动器作用域)。

最佳答案

是的,你可以!

使用以下步骤:

  1. 按照以下所有步骤添加 Google SignIn:https://developers.google.com/identity/sign-in/ios/start-integrating .确保为您的项目启用 Google Drive API。

  2. 初始化登录对象时,不要忘记添加 Google Drive API 范围:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Initialize sign-in
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
    
        GIDSignIn.sharedInstance().delegate = self
    
        // Use here whatever auth scope you wish (e.g., kGTLAuthScopeDriveReadonly, 
        // kGTLAuthScopeDriveMetadata, etc..)
        // You can obviously append more scopes to allow access to more services,
        // other than Google Drive.
        GIDSignIn.sharedInstance().scopes.append(kGTLAuthScopeDrive)
    
        return true
    }
    
  3. 在您的 AppDelegate(或其他一些可访问的地方)中,添加:

    var myAuth: GTMFetcherAuthorizationProtocol? = nil
    
  4. 在您的signIn 委托(delegate)函数中(假设这里它也在AppDelegate 中设置),添加以下代码:

    func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
        if (error == nil) {
            // Logged into google services successfully! 
            // Save relevant details from user.authentication to refresh the token when needed.      
    
            // Set GTMOAuth2Authentication authoriser for your Google Drive service
            myAuth = user.authentication.fetcherAuthorizer()
        } else {
            // Error signing into Google services
        }
    }
    
  5. 最后,无论您在何处设置 GoogleServiceDrive,您都可以设置其授权者,只需设置:

    let service = GTLServiceDrive()
    service.authorizer = (UIApplication.sharedApplication().delegate as! AppDelegate).myAuth
    
  6. 现在您可以使用 Google 的示例代码

    if let authorizer = service.authorizer, canAuth = authorizer.canAuthorize where canAuth {
        // service is authorised and can be used for queries
    } else {
        // service is not authorised 
    }
    

关于ios - 我可以在 iOS 上将 google drive sdk 与来自 google 登录 sdk 的身份验证信息一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187332/

相关文章:

android - 如何修复 Google Drive REST API 的 Proguard 问题

google-apps-script - 使用 Apps 脚本获取文档的审批流程版本名称/编号

c# - 我们可以使用 Google Drive API 创建具有多个工作表的谷歌电子表格吗

python - 为 Google 云端硬盘中上传的私有(private)文件生成可下载链接

android - 如何在Android/iOS中释放组件

ios - OCMock [OCMAnyConstraint isProxy] : message sent to deallocated instance, 发生了什么事?

android - 谷歌云端硬盘同意屏幕卡住

iOS:在哪里可以找到 iOS OSStatus 代码的完整列表?

iphone - xib 的命名约定

ios - ImageView 的边界在 ViewDidLayoutSubviews() 中为零