dtid30526 2017-11-04 21:29
浏览 100
已采纳

如何使用go在OAuth中编写重定向URL

func init() {
    file, err := ioutil.ReadFile("./creds.json")
    if err != nil {
        log.Printf("File error: %v
", 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

  • 写回答

1条回答 默认 最新

  • dongzi5673 2017-11-04 21:44
    关注

    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
    ", 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable