Api 如何使用go登录使用谷歌日历?

Api 如何使用go登录使用谷歌日历?,api,authentication,go,google-calendar-api,backend,Api,Authentication,Go,Google Calendar Api,Backend,我是GO和API的新手,我正在使用GO制作一个后端。 用户应该能够使用他/她的谷歌帐户登录并修改他的日历。 我打开了这个链接上的样本 但我获得客户的方式是通过谷歌给我的密钥 我应该如何让用户登录并获取其日历您需要执行以下操作: import ( "crypto/rand" "encoding/base64" "encoding/gob" "golang.org/x/oauth2/google" "golang.org/x/oauth2" calen

我是GO和API的新手,我正在使用GO制作一个后端。 用户应该能够使用他/她的谷歌帐户登录并修改他的日历。 我打开了这个链接上的样本

但我获得客户的方式是通过谷歌给我的密钥
我应该如何让用户登录并获取其日历

您需要执行以下操作:

import (
    "crypto/rand"
    "encoding/base64"
    "encoding/gob"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2"
    calendar "google.golang.org/api/calendar/v3"
    "github.com/gorilla/sessions"
)
var conf oauth2.Config

func init() {
    gob.Register(&oauth2.Token{})
}

func getLoginURL(state string) string {
    // State can be some kind of random generated hash string.
    // See relevant RFC: http://tools.ietf.org/html/rfc6749#section-10.12
    return conf.AuthCodeURL(state)
}

func randToken() string {
    b := make([]byte, 32) 
    rand.Read(b)
    return base64.StdEncoding.EncodeToString(b)
}

func Login(w http.ResponseWriter, r *http.Request) {
    conf = &oauth2.Config{
        ClientID: "your-client-id",
        ClientSecret: "your-client-secret",
        RedirectUrl: "https://www.yoursite.com/auth",
        Endpoint: google.Endpoint,
        Scopes: []string{"https://www.googleapis.com/auth/calendar"}
    }
    state := randToken()
    sess, _ := session.Get(r, "session")
    sess.Values["state"] = state
    sess.Save(r, w)
    http.Redirect(w, r, conf.AuthCodeURL(state), http.StatusFound)
}

func Auth(w http.ResponseWriter, r *http.Request) {
    sess, _ := session.Get(r, "session")
    state = sess.Values["state"]
    if state != r.URL.Query().Get("state") {
        http.Error(w, "authorization failed", http.StatusUnauthorized)
        return
    }
    tok, _ := conf.Exchange(oauth2.NoContext, c.QueryParam("code"))
    sess.Values["token"] = tok 
    sess.Save(r, w)
    http.Redirect(w, r, "https://www.yoursite.com/profile", http.StatusFound)
}

func GetClient(r *http.Request) *http.Client {
    sess, _ := session.Get(r, "session")
    tok, _ := sess.Values["token"].(*oauth2.Token)
    client := conf.Client(oauth2.NoContext, tok)
    return client
}

func Calendar(w http.ResponseWriter, r *http.Request) {
    client := GetClient(r)
    calendarService, _ = calendar.New(client)
    //do stuff
}
因此,您将它们发送到您的
登录
处理程序,这将生成一个随机密钥,并将其(和用户)发送到google,让他们登录并授权您访问他们的日历,然后将它们重定向到您的
身份验证
处理程序。这将确保他们返回的
状态
密钥与您发送的密钥匹配,如果匹配,将从谷歌获得令牌。然后将其保存到会话中。当您想要获取他们的客户机时,您可以从会话中获取令牌,并使用它交换新的客户机,然后使用它创建日历服务


我还没有完全检查代码,但我尝试从我的应用程序中创建一个基本上使用此代码的示例,因此它应该可以工作(除了可能缺少导入或拼写错误或一些非常小的东西)。

您需要执行以下操作:

import (
    "crypto/rand"
    "encoding/base64"
    "encoding/gob"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2"
    calendar "google.golang.org/api/calendar/v3"
    "github.com/gorilla/sessions"
)
var conf oauth2.Config

func init() {
    gob.Register(&oauth2.Token{})
}

func getLoginURL(state string) string {
    // State can be some kind of random generated hash string.
    // See relevant RFC: http://tools.ietf.org/html/rfc6749#section-10.12
    return conf.AuthCodeURL(state)
}

func randToken() string {
    b := make([]byte, 32) 
    rand.Read(b)
    return base64.StdEncoding.EncodeToString(b)
}

func Login(w http.ResponseWriter, r *http.Request) {
    conf = &oauth2.Config{
        ClientID: "your-client-id",
        ClientSecret: "your-client-secret",
        RedirectUrl: "https://www.yoursite.com/auth",
        Endpoint: google.Endpoint,
        Scopes: []string{"https://www.googleapis.com/auth/calendar"}
    }
    state := randToken()
    sess, _ := session.Get(r, "session")
    sess.Values["state"] = state
    sess.Save(r, w)
    http.Redirect(w, r, conf.AuthCodeURL(state), http.StatusFound)
}

func Auth(w http.ResponseWriter, r *http.Request) {
    sess, _ := session.Get(r, "session")
    state = sess.Values["state"]
    if state != r.URL.Query().Get("state") {
        http.Error(w, "authorization failed", http.StatusUnauthorized)
        return
    }
    tok, _ := conf.Exchange(oauth2.NoContext, c.QueryParam("code"))
    sess.Values["token"] = tok 
    sess.Save(r, w)
    http.Redirect(w, r, "https://www.yoursite.com/profile", http.StatusFound)
}

func GetClient(r *http.Request) *http.Client {
    sess, _ := session.Get(r, "session")
    tok, _ := sess.Values["token"].(*oauth2.Token)
    client := conf.Client(oauth2.NoContext, tok)
    return client
}

func Calendar(w http.ResponseWriter, r *http.Request) {
    client := GetClient(r)
    calendarService, _ = calendar.New(client)
    //do stuff
}
因此,您将它们发送到您的
登录
处理程序,这将生成一个随机密钥,并将其(和用户)发送到google,让他们登录并授权您访问他们的日历,然后将它们重定向到您的
身份验证
处理程序。这将确保他们返回的
状态
密钥与您发送的密钥匹配,如果匹配,将从谷歌获得令牌。然后将其保存到会话中。当您想要获取他们的客户机时,您可以从会话中获取令牌,并使用它交换新的客户机,然后使用它创建日历服务

我还没有完全检查代码,但我尝试从我的应用程序中创建一个简单的示例,它实际上基本上使用了这段代码,所以它应该可以工作(除了可能缺少导入或拼写错误或一些非常小的东西)