英文:
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 (
"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 {
// 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论