一次认证请求适用于双方。

huangapple go评论82阅读模式
英文:

authentication request once for both side

问题

我正在制作一个Chrome扩展程序。

为了方便起见,我希望在Chrome端和服务器端都能获得用户的授权。

在浏览器端,我使用chrome.identity.getAuthToken()方法,在提示后获取一个令牌。

如果可能的话,我如何在服务器端重用这个用户的授权,而不需要再次请求访问权限?

英文:

I'm making a chrome extension.

For convenience, I wish to have the user's authorization on chrome side and the server side.

On the browser side, I use chrome.identity.getAuthToken(), and after a prompt I get a token.

If it's possible, how I can reuse this user's authorization on the server side without asking twice for the access?

答案1

得分: 0

找到了...这是我的答案,谢谢Minty

<!-- language-all: lang-golang -->

import (
    "fmt"
    "net/http"

    "appengine"
    "appengine/urlfetch"
    oauth "code.google.com/p/goauth2/oauth"
    drive "code.google.com/p/google-api-go-client/drive/v2"
)

...

c := appengine.NewContext(r)
transport := &oauth.Transport{
	Token: &oauth.Token{
		AccessToken: "YOUR_TOKEN",
	},
	Transport: &urlfetch.Transport{Context: c},
}
client := transport.Client()
service, err := drive.New(client)
if err != nil {
	// 创建Drive客户端时出错
	http.Error(w, err.Error(), http.StatusInternalServerError)
	return
}
result, err2 := service.About.Get().Do()
if err2 != nil {
	http.Error(w, err2.Error(), http.StatusInternalServerError)
	return
}
fmt.Fprintln(w, result.Name)
英文:

Found... This is my answer, thank you Minty

<!-- language-all: lang-golang -->

import (
    &quot;fmt&quot;
    &quot;net/http&quot;

    &quot;appengine&quot;
    &quot;appengine/urlfetch&quot;
    oauth &quot;code.google.com/p/goauth2/oauth&quot;
    drive &quot;code.google.com/p/google-api-go-client/drive/v2&quot;
)

...

c := appengine.NewContext(r)
transport := &amp;oauth.Transport{
	Token: &amp;oauth.Token{
		AccessToken: &quot;YOUR_TOKEN&quot;,
	},
	Transport: &amp;urlfetch.Transport{Context: c},
}
client := transport.Client()
service, err := drive.New(client)
if err != nil {
	// error creating the Drive client
	http.Error(w, err.Error(), http.StatusInternalServerError)
	return
}
result, err2 := service.About.Get().Do()
if err2 != nil {
	http.Error(w, err2.Error(), http.StatusInternalServerError)
	return
}
fmt.Fprintln(w, result.Name)

huangapple
  • 本文由 发表于 2014年12月8日 21:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/27358787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定