我们已经有一个登录模块使用Google Sign-In sdk。登录成功后,Google登录会提供GIDAuthentication个对象。
现在我想使用google drive sdk访问用户的google驱动器,这需要GTMOAuth2Authentication来提供身份验证信息。那么我可以使用GIDAuthentication
为驱动程序sdk构建GTMOAuth2Authentication吗?
手动分配accessToken
值似乎不起作用(添加了驱动器范围)。
答案 0 :(得分:6)
是的,你可以!
使用以下步骤:
按照以下所述添加Google SignIn的所有步骤:https://developers.google.com/identity/sign-in/ios/start-integrating。确保为项目启用Google Drive API。
初始化登录对象时,请不要忘记添加Google云端硬盘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
委托函数中(假设此处也在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
}
答案 1 :(得分:-1)
/ Creates the auth controller for authorizing access to Google Drive.
- (GTMOAuth2ViewControllerTouch *)createAuthController
{
GTMOAuth2ViewControllerTouch *authController;
authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:@"https://www.googleapis.com/auth/drive"
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
return authController;
}
// Handle completion of the authorization process, and updates the Drive service
// with the new credentials.
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)authResult
error:(NSError *)error
{
if (error != nil)
{
[self showAlert:@"Authentication Error" message:error.localizedDescription];
self.driveService.authorizer = nil;
}
else
{
self.driveService.authorizer = authResult;
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
}-(void)loadDriveFiles
{
NSMutableArray *driveFiles = [[NSMutableArray alloc] init];
NSMutableArray *downloadFiles = [[NSMutableArray alloc] init];
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = [NSString stringWithFormat:@"'%@' IN parents", @"root"];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *files,
NSError *error) {
if (error == nil)
{
for(id key in files.items) {
NSString *titleStr = [key valueForKey:@"title"];
[driveFiles addObject:titleStr];
NSString *downloadStr = [key valueForKey:@"downloadUrl"];
[downloadFiles addObject:downloadStr];
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FilesViewController *filesView = (FilesViewController *)[storyboard instantiateViewControllerWithIdentifier:@"filesView"];
[filesView initwithName:driveFiles anddownload:downloadFiles];
[self.navigationController pushViewController:filesView animated:YES];
}
else
{
NSLog(@"An error occurred: %@", error);
}
}];
}