0

I am working with Gmail API in Go as backend. I made the Gmail sign-in authentication in front end using javascript and sending the auth-code to backend using AJAX Post method in localhost. In backend, I tried to get the Access tokens and Refresh tokens. I used Exchange function to do this. But I am getting the error "oauth2: token expired and refresh token is not set". The Go code for your reference

func GmailCallback(c *fiber.Ctx) error {

googleOauthConfig = &oauth2.Config{
    RedirectURL:  "callback_url",
    ClientID:     "XXXXX",
    ClientSecret: "XXXXXXX",
    Scopes:       []string{"Scope1", "Scope2"},
    Endpoint:     google.Endpoint,
}



code := c.Query("code") 

token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
    log.Println("Error",err)
}

return nil


}

I referred this page reference to create this flow : https://developers.google.com/identity/sign-in/web/server-side-flow

And also i found similar thread here in Node.js. But I do not know how to implement in Go.

It will be great if I get any help for this issue.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ramarasu R
  • 55
  • 5
  • Hi there @RamarasuR! The error `oauth2: token expired and refresh token is not set` declares that something is wrong on the OAuth 2.0 token handling. To circumvent the actual token handling, please take a look at [Gmail API Go quickstart](https://developers.google.com/gmail/api/quickstart/go) and examine its working token management code. Please, use that code in your project and write back sharing your findings. – Jacques-Guzel Heron Oct 13 '20 at 10:06
  • Hi @Jacques-GuzelHeron, Thanks for your reply. In the example you shared, they have done the authentication in Go (server side) as they generated the authCode URL in the console and need to browse that URL in the browser, so will get the authcode which is used for generating the access token. In this flow, we have Go (Server side) callback URL. But In my case, I need to authenticate the user in html/javascript (client side), then I am passing the authcode to Go(Server side) where I used the same method of generating the access token,...... – Ramarasu R Oct 15 '20 at 19:26
  • ... but it did not create the access token, so I was getting the error "oauth2: token expired and refresh token is not set" on further gmail reading codes. I think, the problem might be here: I created the application in developer console using html/javascript(client side) url, authenticating the user and getting the authcode which has been sent to Go (server side) where I have written the code for accessing the emails, here the URL is different from what I have configured in the Developer console application. – Ramarasu R Oct 15 '20 at 19:26
  • ! This behaviour may originate in a faulty communication between the JavaScript code and the Go language. Could you please confirm that the token itself remains unchanged when transfered to Go? To do so please compare them and watch for any modification. Also, please clarify why you create the token in JavaScript just to send it to Go, what is your end goal? There may be simpler approaches to fulfil that goal. – Jacques-Guzel Heron Oct 16 '20 at 14:46
  • Yes, The token remains unchanged. What I need is to access the Gmail API's from GO. I have a front end application nothing but Angular where we have all the page design including Gmail authentication link, when user click it, it will take them to get the gmail consent screen for accessing their Gmail. So I tried with example found here, https://developers.google.com/identity/sign-in/web/server-side-flow – Ramarasu R Oct 18 '20 at 08:38
  • Since you are working with two different languages at the same time, please make sure that you aren't using the objects of one language into another (presumably double check that you aren't working with JavaScript objects in Go). After checking that without improvements, take a look at the Gmail API Go quickstart and test it from scratch. If it works, then you could assume that the problem is in the Javascript/Go communication. Please, test these fixes and write back sharing your findings. – Jacques-Guzel Heron Oct 19 '20 at 10:44

0 Answers0