2017-02-21 280 views
3

我使用谷歌驱动API.When我尝试使用来从驱动文件谷歌云端硬盘API:没有足够的权限的iOS

let query = GTLQueryDrive.queryForFilesList() 
    let service = GTLServiceDrive() 
    query?.pageSize = 1 
    query?.fields = “files” 
    service.executeQuery(
     query!, 
     delegate: self, 
     didFinish: #selector(self.displayResult(ticket:finishedWithObject:error:)) 
) 

我收到错误,如

“的操作不能”完成。(没有足够的权限)。

任何人都知道我需要设置在仪表板这允许吗?

+0

你在谷歌API控制台激活驱动API如果不是激活 –

+0

请检查该https://developers.google .com/drive/v3/web/about-auth –

+0

您使用什么范围进行身份验证? @Lalitkumar不是在开发者控制台错误中未启用。 – DaImTo

回答

0

如果用户访问非公开文件,请务必使用Google登录。或者您对用户相关的文件列表感兴趣。

要这样做,首先请求登录。确保还要设置Google登录的UIDelegate,以在正确的时间点提供登录屏幕:GIDSignInUIDelegate。为了呈现它,您可以拨打:

GIDSignIn.sharedInstance().signIn() 

如果用户曾在已签署的,您可以拨打:

if userSignedInBefore { 
    GIDSignIn.sharedInstance().signInSilently() 
} 

假设你有你的请求被称为在这样一个特定的类(用于说明的想法):

class Requester { 
    static let service = GTLServiceDrive() 

    static func fetchData() { 
     let query = GTLQueryDrive.queryForFilesList() 
     query?.pageSize = 1 
     query?.fields = “files” 
     service.executeQuery(query!, delegate: self, didFinish: #selector(self.displayResult(ticket:finishedWithObject:error:))) 
    } 
    ... 
} 

此外,你需要的GIDSignInDelegate一个代表这个人会照顾和存储用户的相关信息,当用户登录时:

// https://developers.google.com/identity/sign-in/ios/ 
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 
    // Perform any operations when the user disconnects from app here. 
    // ... 
    if (error == nil) { 
     // Perform any operations on signed in user here. 
     //   let userId = user.userID     // For client-side use only! 
     //   let idToken = user.authentication.idToken // Safe to send to the server 
     // .... 
     // Here you will receive an authorizer instance that implements the GTMFetcherAuthorizationProtocol protocol. 
     // This authorizer instance will be required by your GTLRDriveService services as authorizer reference. 
     // https://developers.google.com/identity/sign-in/ios/api/interface_g_i_d_authentication 
     var authorizer = user.authentication.fetcherAuthorizer() 

     // Here you can trigger any callback to now fetch the desired data 
     Requester.service.authorizer = authorizer 
     Requester.fetchData() 
    } else { 
     print("\(error.localizedDescription)") 
    } 
} 

欲了解更多信息或样品实现看看谷歌的文档: https://developers.google.com/drive/v3/web/quickstart/ios

相关问题