我们已经有一个使用 Google Sign-In sdk 的登录模块。 Google 登录给出 GIDAuthentication登录成功后的对象。
现在我想使用需要 GTMOAuth2Authentication 来提供身份验证信息的 google drive sdk 访问用户的 google drive。那么我可以使用 GIDAuthentication
为驱动器 sdk 构造一个 GTMOAuth2Authentication 吗?
手动分配 accessToken
值似乎不起作用(已添加驱动器作用域)。
最佳答案
是的,你可以!
使用以下步骤:
按照以下所有步骤添加 Google SignIn:https://developers.google.com/identity/sign-in/ios/start-integrating .确保为您的项目启用 Google Drive API。
初始化登录对象时,不要忘记添加 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 }
在您的
AppDelegate
(或其他一些可访问的地方)中,添加:var myAuth: GTMFetcherAuthorizationProtocol? = nil
在您的
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 } }
最后,无论您在何处设置 GoogleServiceDrive,您都可以设置其授权者,只需设置:
let service = GTLServiceDrive() service.authorizer = (UIApplication.sharedApplication().delegate as! AppDelegate).myAuth
现在您可以使用 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/