我可以使用谷歌驱动器sdk从谷歌登录sdk在iOS上的身份验证信息? [英] Can I use google drive sdk with authentication info from google sign-in sdk on iOS?
问题描述
我们已经有一个登录模块使用Google Sign-In sdk。登录成功后,Google登录会提供 GIDAuthentication 对象。
现在我想通过Google Drive sdk访问用户的谷歌驱动器,它需要一个GTMOAuth2Authentication来提供身份验证信息。那么我可以使用 GIDAuthentication
为drive sdk构造一个GTMOAuth2Authentication吗?
手动分配 accessToken
值似乎不起作用(已添加驱动器范围)。
是的,您可以!
使用以下步骤: / p>
-
按照以下步骤添加Google SignIn: https://developers.google.com/identity/sign-in/ios/start-integrating 。请确保您为项目启用了Google Drive API。
当初始化登录对象时,请不要忘记添加Google Drive API范围: -
在您的
AppDelegate
(或其他可访问的地方)中,添加:
var myAuth:GTMFetcherAuthorizationProtocol? =无
-
在
signIn 委托函数(假设它也在 AppDelegate
中设置),请添加以下代码:
func signIn(signIn:GIDSignIn !, didSignInForUser user:GIDGoogleUser !, withError error:NSError!){
if(error == nil){
//成功登录到Google服务!
//保存来自user.authentication的相关详细信息,以便在需要时刷新令牌。
//为您的Google云端硬盘服务设置GTMOAuth2Authentication授权者
myAuth = user.authentication.fetcherAuthorizer()
}其他{
//登录Google服务时出错
$ / code $ / pre $
最后,无论您设置GoogleServiceDrive,也可以设置其授权者,只需设置:
let service = GTLServiceDrive()
service.authorizer =(UIApplication .sharedApplication()。delegate as!AppDelegate).myAuth
-
现在您可以使用示例代码由Google
if let authorizer = service.authorizer,canAuth = authorizer.canAuthorize where canAuth {
// service被授权并且可以用于查询
} else {
//服务未被授权
}
func application(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {
//初始化登录
var configureError:NSError?
GGLContext.sharedInstance()。configureWithError(& configureError)
assert(configureError == nil,配置Google服务时出错:\(configureError))
GIDSignIn。 sharedInstance()。delegate = self
//在这里使用你希望的任何授权范围(例如,kGTLAuthScopeDriveReadonly,
// kGTLAuthScopeDriveMetadata等)
//你可以明显附加更多范围以允许访问更多服务,
// Google Drive除外。
GIDSignIn.sharedInstance()。scopes.append(kGTLAuthScopeDrive)
return true
}
We already have a login module uses Google Sign-In sdk. Google Sign-In gives a GIDAuthentication object after login succeed.
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?
Manually assign the accessToken
value seems not working(drive scope added).
Yes you can!
Use the following steps:
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.
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 }
In your
AppDelegate
(or some other accessible place), add:var myAuth: GTMFetcherAuthorizationProtocol? = nil
In you
signIn
delegate function (assuming here it is also set inAppDelegate
), 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 } }
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
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 }
这篇关于我可以使用谷歌驱动器sdk从谷歌登录sdk在iOS上的身份验证信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!