dongpangfu6322 2013-04-22 11:30
浏览 53
已采纳

如何在Go中的App Engine上为OAuth2使用客户端ID?

I have some fairly simple Go code running in AppEngine that should be using OAuth2 to fetch the list of files from the user's account. It seems to initialize the service OK but when it tries to fetch the file list, I get this error: OAuthError: RoundTrip: no Token supplied

package foo

import (
    "appengine"
    "appengine/urlfetch"
    "code.google.com/p/goauth2/oauth"
    "code.google.com/p/google-api-go-client/drive/v2"
    "fmt"
    "net/http"
)

var config = &oauth.Config{
    ClientId:     "(redacted).apps.googleusercontent.com",
    ClientSecret: "REDACTED",
    Scope:        "https://www.googleapis.com/auth/drive",
    AuthURL:      "https://accounts.google.com/o/oauth2/auth",
    TokenURL:     "https://accounts.google.com/o/oauth2/token",
}

func init() {
    http.HandleFunc("/", home)
}

func home(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    transport := &oauth.Transport{
        Config:    config,
        Transport: &urlfetch.Transport{Context: c}}
    svc, err := drive.New(transport.Client())
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    q := svc.Files.List()
    _, err = q.Do()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    fmt.Fprintf(w, "Success!")
}

I cannot figure out what I'm doing wrong here. Any help would be kindly appreciated.

  • 写回答

2条回答 默认 最新

  • dsh102123 2013-04-22 16:50
    关注

    The token configuration is not enough; you first have to get a valid access token with the following steps:

    1. Redirect the user to the page returned by AuthCodeURL. The user will be shown the name of your application and the requested permissions.

    2. If the user grants the permissions, they will be redirected to the RedirectURL you gave in the configuration. The URL will contain a query parameter named code.

    3. Retrieve the code parameter and pass it to Exchange. If everything went well, the requests should now be authenticated properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?