【问题标题】:Google Classroom API integration with swiftGoogle Classroom API 与 swift 的集成
【发布时间】:2020-08-15 18:02:26
【问题描述】:

我想在 swift 上将我的 iOS 应用与 Google Classroom 相关联。我目前的目标是能够获得我注册的所有课程的列表。

这是我目前使用的代码


        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 下,但我真的不明白我应该做什么。作为参考,我正在使用此身份验证范围。 “https://www.googleapis.com/auth/classroom.courses”

【问题讨论】:

    标签: swift rest google-api google-classroom


    【解决方案1】:

    首先您必须使用 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()
    }
    

    【讨论】:

    • 我已经实现了 Google 登录,我可以使用这个 Google 云端硬盘选择器 [github.com/ConfusedVorlon/HSGoogleDrivePicker] 访问 Google 云端硬盘,并且在访问我的应用程序时会通过登录屏幕。
    • 你是否也设置了googleClassroomService.authorizer = user.authentication.fetcherAuthorizer()?我将包含我的 sign() 方法来展示它是如何做到的。
    • 哈哈太感谢了,问题是我在代码中初始化了googleClassroomService两次,所以重置了授权器,谢谢!但是我的代码正在运行“错误:响应不是文件列表”,您对为什么会这样有任何想法吗?
    • 你得到的是 GTLRClassroom_ListCoursesResponse,而不是 GTLRClassroomQuery_CoursesList。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多