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.

6
  • 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 and examine its working token management code. Please, use that code in your project and write back sharing your findings. Commented Oct 13, 2020 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,...... Commented Oct 15, 2020 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. Commented Oct 15, 2020 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. Commented Oct 16, 2020 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, developers.google.com/identity/sign-in/web/server-side-flow Commented Oct 18, 2020 at 8:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.