Amr Khalil
Amr Khalil

Reputation: 135

how to write Redirect URL in OAuth using go

func init() {
    file, err := ioutil.ReadFile("./creds.json")
    if err != nil {
        log.Printf("File error: %v\n", err)
        os.Exit(1)
    }
    json.Unmarshal(file, &cred)

    conf = &oauth2.Config{
        ClientID:     cred.Cid,
        ClientSecret: cred.Csecret,
        RedirectURL:  "http://127.0.0.1:3000/auth",
        Scopes: []string{
            "https://www.googleapis.com/auth/userinfo.email", // You have to select your own scope from here -> https://developers.google.com/identity/protocols/googlescopes#google_sign-in
            "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/calendar.readonly",
        },
        Endpoint: google.Endpoint,
    }
}

the above code is the init function in my code the part RedirectURL: "http://127.0.0.1:3000/auth" uses the localhost link but what happens if I deploy my site using heruko shouldn't the 127.0.0.1 be changed if yes how should I change it

Upvotes: 1

Views: 516

Answers (1)

mkopriva
mkopriva

Reputation: 38313

Heroku recommends using environment variables for most configuration, so you can use the os package to retrieve the redirect url from the environment.

func init() {
    redirURL := os.Getenv("OAUTH_REDIRECT_URL")

    file, err := ioutil.ReadFile("./creds.json")
    if err != nil {
        log.Printf("File error: %v\n", err)
        os.Exit(1)
    }
    json.Unmarshal(file, &cred)

    conf = &oauth2.Config{
        ClientID:     cred.Cid,
        ClientSecret: cred.Csecret,
        RedirectURL:  redirURL,
        Scopes: []string{
            "https://www.googleapis.com/auth/userinfo.email", // You have to select your own scope from here -> https://developers.google.com/identity/protocols/googlescopes#google_sign-in
            "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/calendar.readonly",
        },
        Endpoint: google.Endpoint,
    }
}

And then on your local machine set an environment variable like so:

export OAUTH_REDIRECT_URL=http://127.0.0.1:3000/auth

and on your heroku dyno set it to the value you need it to redirect to.

Upvotes: 1

Related Questions