问题描述
我想快速将我的iOS应用程序与Google课堂相关联。我目前的目标是能够获得我所报名的所有课程的清单。
这是我当前正在使用的代码
func googleClassroomList() {
//let sharedInstance = GIDSignIn.sharedInstance()
//let handler = sharedInstance
googleClassroomService.apiKey = "AIzaSyBOGamjhRuu45T2jT7Qa3LmtntSwgIxeqo"
let query = GTLRClassroomQuery_CoursesList.query()
query.pageSize = 1000
let classQuery = googleClassroomService.executeQuery(query,completionHandler: { ticket,fileList,error in
if error != nil {
let message = "Error: \(error?.localizedDescription ?? "")"
print(message)
} else {
if let list = fileList as? GTLRClassroomQuery_CoursesList {
self.fileList = list
print("List: \(list)")
}
else {
print("Error: response is not a file list")
}
}
}
)
}
这是错误消息:
Error: Request is missing required authentication credential. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
我不知道我在哪里附加了OAuth访问令牌,我尝试将其放在apiKey下,但是我真的不明白我该怎么做。供参考,我正在使用此auth范围。 “ https://www.googleapis.com/auth/classroom.courses”
解决方法
首先,您必须使用Google登录,然后Google将为您管理OAuth 2.0令牌。
在这里,您有说明: https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift
还要确保在实际登录时设置service.authorizer属性:
func sign(_ signIn: GIDSignIn!,didSignInFor user: GIDGoogleUser!,withError error: Error!) {
guard let user = user else {
return
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.setLoggedOut(loggedOut: false)
self.myClassroom.service.authorizer = user.authentication.fetcherAuthorizer()
self.showClassroomClasses()
}