本文介绍了获取NSError域:" com.google.GTLJSONRPCErrorDomain"在Swift中获取Google日历事件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试将日历事件获取到我的iOS应用程序。因此,为此,我在Google开发人员控制台中创建了一个项目,并获得了客户端ID ApiKey。

I am trying to fetch Calendar events to my iOS application. So, for that I have create a project in Google developers console and got Client ID, ApiKey.

并使用

pod 'GoogleAPIClient/Calendar', '~> 1.0.2'
pod 'GTMOAuth2', '~> 1.1.0'
pod 'Google/SignIn'


并在登录Gmail后,要求其访问日历的权限,直到工作正常为止,但是,同时获取显示以下错误的事件。

And after signing to Gmail, its asking permission to access Calendar, till that working fine, but, while fetching the events showing following error.

error   NSError domain: "com.google.GTLJSONRPCErrorDomain" - code: 401  0x0000604000646330

代码如下:

private let scopes = [kGTLAuthScopeCalendar]

private let service = GTLServiceCalendar()

private let kApiKey = "someKey"

let output = UITextView()

private let kKeychainItemName = "Google Calendar API"

private let kClientID = "SomeID"

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

    if (error == nil) {


    } else {
        print("\(error.localizedDescription)")
    }

    if user != nil {
        output.frame = view.bounds
        output.isEditable = false
        output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
        output.autoresizingMask = [.flexibleHeight, .flexibleWidth]

        view.addSubview(output);

        if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychain(
            forName: kKeychainItemName,
            clientID: kClientID,
            clientSecret: nil) {
            service.authorizer = auth
        }

        print("\(user)")
        service.authorizer = user.authentication.fetcherAuthorizer()
        fetchEvents()
    }

}

func fetchEvents() {

    let query = GTLQueryCalendar.queryForEventsList(withCalendarId: "primary")

    query?.maxResults = 10
    query?.singleEvents = true
    query?.orderBy = kGTLCalendarOrderByStartTime
    service.executeQuery(
        (query)!,
        delegate: self,
        didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
}

// Display the start dates and event summaries in the UITextView

@objc func displayResultWithTicket(

    ticket: GTLServiceTicket,
    finishedWithObject response : GTLCalendarEvents,
    error : NSError?) {

    if let error = error {
        showAlert(title: "Error", message: error.localizedDescription)
        return
    }

    var eventString = ""

    if let events = response.items(), !events.isEmpty {
        for event in events as! [GTLCalendarEvent] {
            print(event)
        }
    } else {
    print("No upcoming events found.")
  }
}

   func showAlert(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Thank You", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)

    }

override func viewDidLoad() {
    super.viewDidLoad()

    GIDSignIn.sharedInstance().clientID = kClientID
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().scopes = scopes
    GIDSignIn.sharedInstance().signIn()
    GIDSignIn.sharedInstance().delegate = self
    service.apiKey = kApiKey
}

即使我检查了几个论坛,也没有得到任何解决方案。

Even I checked few forums, I did not get any solution for this.


推荐答案

迅速-4

我不知道您为什么会收到该错误。
我认为您没有使用最新的广告连播。
请参阅以下代码,该代码已用于获取Google日历事件。

I doesn't recognise why you are getting that error.I think you are not using latest pods.Refer following code, that I have used for fetch google calendar events.

窗格:-
pod'GoogleAPIClientForREST / Calendar','〜> 1.2.1'

Pods: - pod 'GoogleAPIClientForREST/Calendar', '~> 1.2.1'

pod'Google / SignIn','〜> 3.0.3'

pod 'Google/SignIn', '~> 3.0.3'

import UIKit
import GoogleAPIClientForREST
import GoogleSignIn
class ClassForCalEvent: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {

    let eventArray = NSMutableArray()
    private let scopes = [kGTLRAuthScopeCalendar]

    private let service = GTLRCalendarService()
    override func viewDidLoad() {
        super.viewDidLoad()

          // Configure Google Sign-in.
            GIDSignIn.sharedInstance().delegate = self
            GIDSignIn.sharedInstance().uiDelegate = self
            GIDSignIn.sharedInstance().scopes = scopes
            GIDSignIn.sharedInstance().signIn()

    }

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
        if let error = error {
            showAlert(title: "Authentication Error", message: error.localizedDescription)
            self.service.authorizer = nil
        } else {
                        self.service.authorizer = user.authentication.fetcherAuthorizer()
            fetchEvents()
        }
    }

    // Construct a query and get a list of upcoming events from the user calendar
    func fetchEvents() {
        let query = GTLRCalendarQuery_EventsList.query(withCalendarId: "primary")
        query.maxResults = 10
        query.timeMin = GTLRDateTime(date: Date())
        query.singleEvents = true
        query.orderBy = kGTLRCalendarOrderByStartTime
        service.executeQuery(
            query,
            delegate: self,
            didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
    }

    // Display the start dates and event summaries in the UITextView
    @objc func displayResultWithTicket(
        ticket: GTLRServiceTicket,
        finishedWithObject response : GTLRCalendar_Events,
        error : NSError?) {

        if let error = error {
            showAlert(title: "Error", message: error.localizedDescription)
            return
        }

        var outputText = ""
        if let events = response.items, !events.isEmpty {
            for event in events {
                let start = event.start!.dateTime ?? event.start!.date!
                let startString = DateFormatter.localizedString(
                    from: start.date,
                    dateStyle: .short,
                    timeStyle: .short)

                let dataDic = ["start_Time":startString , "tag":"red", "Event_Name":event.summary!]
                eventArray.add(dataDic)

                outputText += "\(startString) - \(event.summary!)\n"
                print("Output ", startString , event )
            }
            UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: eventArray), forKey: "EventArray")

            UserDefaults.standard.synchronize()
        } else
        {
            outputText = "No upcoming events found."
        }
        print("Output Text",outputText)
        showAlert(title: "Event Sync Succesfully!", message:"")
    }

这篇关于获取NSError域:" com.google.GTLJSONRPCErrorDomain"在Swift中获取Google日历事件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:17