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

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

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 -->

  1. import (
  2. "fmt"
  3. "net/http"
  4. "appengine"
  5. "appengine/urlfetch"
  6. oauth "code.google.com/p/goauth2/oauth"
  7. drive "code.google.com/p/google-api-go-client/drive/v2"
  8. )
  9. ...
  10. c := appengine.NewContext(r)
  11. transport := &oauth.Transport{
  12. Token: &oauth.Token{
  13. AccessToken: "YOUR_TOKEN",
  14. },
  15. Transport: &urlfetch.Transport{Context: c},
  16. }
  17. client := transport.Client()
  18. service, err := drive.New(client)
  19. if err != nil {
  20. // 创建Drive客户端时出错
  21. http.Error(w, err.Error(), http.StatusInternalServerError)
  22. return
  23. }
  24. result, err2 := service.About.Get().Do()
  25. if err2 != nil {
  26. http.Error(w, err2.Error(), http.StatusInternalServerError)
  27. return
  28. }
  29. fmt.Fprintln(w, result.Name)
英文:

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

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

  1. import (
  2. &quot;fmt&quot;
  3. &quot;net/http&quot;
  4. &quot;appengine&quot;
  5. &quot;appengine/urlfetch&quot;
  6. oauth &quot;code.google.com/p/goauth2/oauth&quot;
  7. drive &quot;code.google.com/p/google-api-go-client/drive/v2&quot;
  8. )
  9. ...
  10. c := appengine.NewContext(r)
  11. transport := &amp;oauth.Transport{
  12. Token: &amp;oauth.Token{
  13. AccessToken: &quot;YOUR_TOKEN&quot;,
  14. },
  15. Transport: &amp;urlfetch.Transport{Context: c},
  16. }
  17. client := transport.Client()
  18. service, err := drive.New(client)
  19. if err != nil {
  20. // error creating the Drive client
  21. http.Error(w, err.Error(), http.StatusInternalServerError)
  22. return
  23. }
  24. result, err2 := service.About.Get().Do()
  25. if err2 != nil {
  26. http.Error(w, err2.Error(), http.StatusInternalServerError)
  27. return
  28. }
  29. 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:

确定