Swift Gmail API: Try to send message gives error -1001

445 Views Asked by At

I'm trying to send a gmail message in Swift using the GoogleAPIClientForREST and GTMOAuth2 libraries but I'm getting the aforementioned error.

Here's my code:

    let message = GTLRGmail_Message()
    message.raw = "test"
    let query = GTLRGmailQuery_UsersMessagesSend.query(withObject: message, userId: "[email protected]", uploadParameters: nil)
    let gmailService = GTLRGmailService()
    let auth = GTMOAuth2Authentication()
    auth.clientID = gmailClientID
    gmailService.authorizer = auth
    gmailService.executeQuery(query) { ticket, object, error in
        print(ticket)
        print(object)
        print(error)
    }

and here's the error:

Optional(Error Domain=com.google.GTMOAuth2 Code=-1001 "(null)" UserInfo={request=<NSMutableURLRequest: 0x600000016ed0> { URL: https://www.googleapis.com/gmail/v1/users/example%40gmail.com/messages/send?prettyPrint=false }})

Notes:

1

There are 1 best solutions below

2
abielita On

Make sure that you've integrated GTMOAuth2 properly into your app by following this tutorial. Be sure to register the Platform as Other and NOT iOS otherwise you'll get "invalid_client" error when authenticating.

You may also check the given solution on this thread.