简体   繁体   English

如何使用Go登录以使用您的Google日历?

[英]how to login to use your google calendar using go?

I am new to GO and APIs and I am making a back end using GO. 我是GO和API的新手,并且正在使用GO做后端。 the user should be able to login using his/her google account and modifies his calendar. 用户应该能够使用其Google帐户登录并修改其日历。 I opened the sample on this link Google Quickstart 我在此链接上打开了示例Google快速入门

but the way I get the client is by the keys google gives it to me how should I make the user login and get his calendar 但是我获取客户的方式是通过google给我的键,我该如何使用户登录并获取他的日历

You'll need to do something like: 您需要执行以下操作:

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
}

So, you send them to your Login handler, this generates a random key, and sends it (and the user) to google to have them login and authorize you to access their calendars, which will then redirect them to you Auth handler. 因此,您将它们发送到Login处理程序,这将生成一个随机密钥,并将其(和用户)发送给google以让他们登录并授权您访问其日历,然后将其重定向到您的Auth处理程序。 This will make sure that the state key they sent back matches the one you sent, and if so, will get the token from Google. 这样可以确保他们发送回的state密钥与您发送的state密钥相匹配,如果是,则将从Google获取令牌。 You then save it to the session. 然后,将其保存到会话中。 When you want to get their client, you fetch the token from your session, and use it to exchange for a new Client, which you then use to create your Calendar service. 当您想要获取他们的客户端时,您可以从会话中获取令牌,并使用它来交换新的客户端,然后使用该客户端来创建日历服务。

I haven't checked the code exactly, but I tried to make a minimal example from my app which actually uses basically this code, so it should work (aside from probably missing an import or typos or some really minor stuff). 我还没有完全检查代码,但是我尝试从我的应用程序中创建一个最小的示例,该示例实际上使用了此代码,因此它应该可以工作(除了可能会丢失导入或错别字或一些非常小的东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM