<p>I would like to write a server in go that allows Facebook login or creating an account on our server. Are there any go libraries that would help with this or has anyone had any experience doing something similar?</p>
<hr/>**评论:**<br/><br/>bear1728: <pre><p>I just wrote a google login system last night actually! The code is rough, but I can tell you what I did for that. It may be similar?</p>
<p>I only used "golang.org/x/oauth2" (<a href="https://godoc.org/golang.org/x/oauth2">https://godoc.org/golang.org/x/oauth2</a>)</p>
<p>I followed googles instruction on the client, where they give you a button and callback. When the user presses the button and logs in, your callback function receives a one time authorization code. I send that code to the server.</p>
<p>The server has an oauth2 config struct set up like this</p>
<pre><code>authConf = &oauth2.Config{
ClientID: gKeys.ID,
ClientSecret: gKeys.Secret,
Scopes: []string{
"email",
},
RedirectURL: "postmessage",
Endpoint: google.Endpoint,
}
</code></pre>
<p>Once the server has the code, I exchanged the code for a token with this:</p>
<pre><code> t, err := authConf.Exchange(oauth2.NoContext, code)
</code></pre>
<p>The variable <code>t</code> is now an oauth2 token. You can inspect it manually or maybe facebook has nice documentation about it. But what I found with google is that this includes the id_token. All you have to do is extract the user's information from this token. I extract this with</p>
<pre><code> claims := t.Extra("id_token").(string)
</code></pre>
<p>Now claims is a "." delimited list of base64 encoded JSON objects, one of which is the actual identity. I'm pretty sure there is a package that extracts the stuff you want, but it was easy enough to do by hand.</p>
<p>The one weird part was that the JSON I got back was not correctly formatted. I had to add on additional "=" (see the wiki page on base64) characters until the length was divisible by 4 until Go <code>base64.StdEncoding.DecodeString</code> actually worked. I guess it saves space if they let us add the <code>=</code> ourselves.</p>
<p>I think using 3rd party login systems is nice because you don't have to ask users for passwords. Unless you're using API calls specifically from facebook (monitoring users friends or something) this should be sufficient for a login system I suspect. Unless I'm missing something.</p>
<p>I would love feedback as well if anyone has anything to say. I'm also new to this oauth sign-in system.</p></pre>srikanthegdee: <pre><p>where did you get the "code" from?</p></pre>bear1728: <pre><p>I got a lot mostly from the docs: <a href="https://godoc.org/golang.org/x/oauth2#Config" rel="nofollow">https://godoc.org/golang.org/x/oauth2#Config</a></p>
<p>The rest was kind of just playing around with the response and digging through the godocs for the auth package and the google authentication guide: <a href="https://developers.google.com/identity/protocols/OpenIDConnect#exchangecode" rel="nofollow">https://developers.google.com/identity/protocols/OpenIDConnect#exchangecode</a></p>
<p>It also helped to look at other packages which do something similar like this one: <a href="https://github.com/markbates/goth/blob/master/providers/gplus/gplus.go" rel="nofollow">https://github.com/markbates/goth/blob/master/providers/gplus/gplus.go</a></p></pre>srikanthegdee: <pre><p>Thanks, but I was referring to </p>
<blockquote>
<p>t, err := authConf.Exchange(oauth2.NoContext, code)</p>
</blockquote>
<p>How or from where do you derive the value of "code" variable?</p></pre>bear1728: <pre><p>Oh I thought you were referring to the sketchy code blocks.</p>
<p>The code variable I got from the web client. It's sent to the server via some ajax call. I followed these instructions for that: <a href="https://developers.google.com/identity/sign-in/web/server-side-flow" rel="nofollow">https://developers.google.com/identity/sign-in/web/server-side-flow</a></p>
<p>I can send you that code if you're interested.</p></pre>Yorirou: <pre><p>I use this library: <a href="https://github.com/golang/oauth2">https://github.com/golang/oauth2</a>
It even has a facebook subpackage for the endpoint information.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传