连接Google Drive API出错(Swift 5)

3
我想在我的应用程序中连接Google Drive API,以显示用户文件列表并能够将它们下载到设备上。我正在遵循这个示例集成Google Drive到iOS应用程序
我连接了Google SDK并成功授权了用户。但是无论如何都无法获取其文件列表。我不断收到以下错误消息:
"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
我多次检查了我的应用程序和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()
     })
}

API is enable The limit is not exhausted Authorization successful


kGTLRAuthScopeDrive的值是多少? - Linda Lawton - DaImTo
2个回答

2
解决了。感谢所有帮助我的人。 我所需要做的就是将用户身份验证状态传输到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
}()
}

现在的代码看起来不太好,我会想办法改进它。但这已经可以工作了,我得到了用户文件列表。


1

"每日未经身份验证使用限制已超出。

这意味着您没有得到授权。调用公开用户私有数据的 Google API 必须包含一个授权头 Bearer 令牌,其中包含访问令牌。

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

调试尝试

请注意,我从未拥有过苹果设备。我不是 iOS 或 Swift 开发人员。我已经在使用 Google API 和库进行工作多年。

从我所看到的内容,您正在调用:

fileListTicket = driveService.executeQuery(query,

在您正在跟随的教程中,调用

self.service.executeQuery(query)

你没有改变精细的设置。
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self

为什么你改了它?

我不明白我在哪里没有授权,因为我收到了用户已经授权并且我也收到了他的令牌的消息。 - VyacheslavBakinkskiy
driveService.executeQuery( <-- 这个调用没有授权。很抱歉,我不是iOS开发人员,无法提供太多帮助。driveService需要以某种方式访问凭据。 - Linda Lawton - DaImTo
我的意思是...你正在接收自己打印的消息。你确实获得了访问资源所需的令牌,但你没有在其他任何地方使用它。此外,这个 access_token 是敏感信息,请不要像你已经做过的那样分享,因为当它处于活动状态时,它可以用来访问你的所有信息(受范围限制)。 - Raserhin
@raserhin 说得好,但它只有一个小时的有效期,现在已经过期了。 - Linda Lawton - DaImTo
那个页面使用了self.service.executeQuery(query),你为什么移除了delegate? - Linda Lawton - DaImTo
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接