gpt4 book ai didi

ios - 连接到 Google Drive API 时出错(Swift 5)

转载 作者:行者123 更新时间:2023-12-01 15:44:10 29 4
gpt4 key购买 nike

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

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

"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."

我多次在 Google 控制台中检查我的应用程序和设置,一步步完成,但仍然无法解决这个问题。有没有人遇到过同样的问题?

我的代码和截图:

//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

最佳答案

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

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

关于ios - 连接到 Google Drive API 时出错(Swift 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63896492/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com