我可以在 iOS 上使用来自 google 登录 sdk 的身份验证信息的 google drive sdk 吗? [英] Can I use google drive sdk with authentication info from google sign-in sdk on iOS?
问题描述
我们已经有一个使用 Google Sign-In sdk 的登录模块.登录成功后,Google 登录会提供 GIDAuthentication 对象.
We already have a login module uses Google Sign-In sdk. Google Sign-In gives a GIDAuthentication object after login succeed.
现在我想使用 google drive sdk 访问用户的 google drive,它需要 GTMOAuth2Authentication 来提供身份验证信息.那么我可以使用 GIDAuthentication
为驱动 sdk 构建 GTMOAuth2Authentication 吗?
Now I want to access user's google drive with google drive sdk which needs a GTMOAuth2Authentication to provide authentication info. So can I use the GIDAuthentication
to construct a GTMOAuth2Authentication for drive sdk?
手动分配 accessToken
值似乎不起作用(添加了驱动范围).
Manually assign the accessToken
value seems not working(drive scope added).
推荐答案
可以!
使用以下步骤:
按照以下说明添加 Google 登录的所有步骤:https://developers.google.com/identity/sign-in/ios/start-integrating.确保为您的项目启用 Google Drive API.
Follow all the steps to add Google SignIn as described in: https://developers.google.com/identity/sign-in/ios/start-integrating. Make sure you enable the Google Drive API for your project.
初始化登录对象时,别忘了添加 Google Drive API 作用域:
When initialising the sign-in object, don't forget to add the Google Drive API scope:
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
(或其他可访问的地方)中,添加:
In your AppDelegate
(or some other accessible place), add:
var myAuth: GTMFetcherAuthorizationProtocol? = nil
在你的signIn
委托函数中(假设这里也设置在AppDelegate
中),添加如下代码:
In you signIn
delegate function (assuming here it is also set in AppDelegate
), add the following code:
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,您也可以设置其授权者,只需设置:
Last, wherever you set your GoogleServiceDrive, you can also set its authoriser, simply by setting:
let service = GTLServiceDrive()
service.authorizer = (UIApplication.sharedApplication().delegate as! AppDelegate).myAuth
现在您可以使用 Google 提供的示例代码
Now you can use the example code by 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 上使用来自 google 登录 sdk 的身份验证信息的 google drive sdk 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!