【问题标题】:Error connecting to Google Drive API (Swift 5)连接到 Google Drive API (Swift 5) 时出错
【发布时间】:2020-09-16 18:31:29
【问题描述】:

我想在我的应用程序中连接到 Google Drive API,以便显示用户文件列表并能够将它们下载到设备。我正在关注这个示例Integrate Google Drive to iOS app

我已连接 Google SDK 并成功授权用户。但是我无法以任何方式获取其文件列表。我不断收到以下错误:

“已超过未经验证使用的每日限制。继续使用需要注册。”

我在 Google Console 中检查了我的应用程序和设置很多次,一步一步地做了一切,但仍然无法解决这个问题。有没有人遇到过同样的问题?

我的代码和截图:

//class AppDelegate...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GIDSignIn.sharedInstance().clientID = "Me client ID"
        return true
}

//class myVC: GIDSignInDelegate...
override func viewDidLoad() {
        super.viewDidLoad() 
        GIDSignIn.sharedInstance().presentingViewController = self
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().scopes = [kGTLRAuthScopeDrive]
        GIDSignIn.sharedInstance().restorePreviousSignIn()
}

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
        if let error = error {
            print("Google autorization error: \(error.localizedDescription)")
            return
        }
        guard let token = user.authentication.accessToken else { return }
    SourceAuthorizationStateManager.shared.addAuthorizedSource(.googleDrive)
    
    let fullName = user.profile.name
    print("Google authorization successful. User name: \(fullName ?? "Error: no user name")\nUser token: \(token)")
}

//class GoogleDriveFileListSource...
private var fileListTicket: GTLRServiceTicket?
var files: [FileModelProtocol] {
        guard let files = fileList?.files else { return [] }
        return files.map { GoogleDriveFileModel($0) }
}

lazy var driveService: GTLRDriveService = {
    let service = GTLRDriveService()
    service.shouldFetchNextPages = true
    service.isRetryEnabled = true
    return service
}()

func fetchFileList(path: String?, _ completion: @escaping () -> Void) {
        let query = GTLRDriveQuery_FilesList.query()
        query.fields = "kind,nextPageToken,files(mimeType,id,kind,name,webViewLink,thumbnailLink,trashed)"
        
        fileListTicket = driveService.executeQuery(query,
                                                   completionHandler: { [weak self] (_, resultObject, error) in
                                                    if let error = error {
                                                        debugPrint("driveService.executeQuery error: \(error.localizedDescription)")
                                                        return
                                                    }
                                                    
                                                    guard let self = self,
                                                        let fileList = resultObject as? GTLRDrive_FileList else { return }
                                                    
                                                    self.fileList = fileList
                                                    self.fileListTicket = nil
                                                    
                                                    completion()
     })
}

【问题讨论】:

  • kGTLRAuthScopeDrive 的值是多少

标签: ios swift google-drive-api


【解决方案1】:

解决了。感谢所有帮助过的人。 我所要做的就是将用户身份验证状态传输到 driveService 并将 GTMSessionFetcher 导入到我的文件中。

var googleUser: GIDGoogleUser?
class myVC: GIDSignInDelegate {
...
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
        if let error = error {
            print("Google autorization error: \(error.localizedDescription)")
            return
        }
        guard let token = user.authentication.accessToken else { return }
    SourceAuthorizationStateManager.shared.addAuthorizedSource(.googleDrive)
    googleUser = user   //Here I have saved the current user

    let fullName = user.profile.name
    print("Google authorization successful. User name: \(fullName ?? "Error: no user name")\nUser token: \(token)")
}
}

class GoogleDriveFileListSource {
...
lazy var driveService: GTLRDriveService = {
    let service = GTLRDriveService()
    if let user = googleUser {
    service.authorizer = user.authentication.fetcherAuthorizer() //Here I passed the status
    }
    service.shouldFetchNextPages = true
    service.isRetryEnabled = true
    return service
}()
}

现在代码看起来不是很好,我会考虑如何改进它。但这已经有效,我得到了用户文件的列表。

【讨论】:

    【解决方案2】:

    “已超出未经验证使用的每日限制。

    表示您未获得授权。对公开私人用户数据的 Google API 的调用必须包含一个包含访问令牌的授权标头不记名令牌。

    您似乎正在发送一种称为用户令牌的东西,我认为它与设置授权标头不同。

    调试尝试

    注意,我从未拥有过苹果设备。我不是 Ios 或 swift 开发人员。我多年来一直在使用 Google API 和库。

    据我所知你在打电话

    fileListTicket = driveService.executeQuery(query,
    

    在教程期间您正在关注电话

    self.service.executeQuery(query)
    

    你没有改变微妙的设置

    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    

    你为什么改变它?

    【讨论】:

    • 我不明白我在哪里没有被授权,因为我收到一条消息说用户被授权并且我收到了他的令牌。
    • driveService.executeQuery(
    • 我的意思是...您正在收到自己打印的消息。您确实获得了访问资源所需的令牌,但您没有在其他任何地方使用。此外,此access_token 是敏感信息,请勿像您所做的那样共享,因为当它处于活动状态时,它可用于访问您的所有信息(受范围限制)。
    • @raserhin 好点,但它只在一个小时内有效,现在已经过期了
    • @raserhin 感谢有关令牌的警告,在屏幕截图中它被剪掉了。我按照谷歌文档和第三方指南中的指示做所有事情,例如,uynguyen.github.io/2019/02/15/Integrate-Google-Drive-to-iOS-app 我看不到这里使用令牌的位置,在其他网站上也是如此。
    猜你喜欢
    • 1970-01-01
    • 2016-02-24
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 2018-01-17
    • 2020-03-25
    相关资源
    最近更新 更多