我正在使用gosimple/oauth2包來處理用戶的OAuth2登錄。我沒有任何問題,GitHub example code,它完美的作品,並返回我想要的。gosimple/oauth2和Google OAuth2
但是,我有問題讓它與Google合作。我已經正確定義我的範圍(據我可以看到),而我得到令牌的響應,但一旦我進入它,在〜68行
package main
import (
"bitbucket.org/gosimple/oauth2"
"flag"
"fmt"
"io"
"log"
"os"
)
var (
// Register new app at https://github.com/settings/applications and provide
// clientId (-id), clientSecret (-secret) and redirectURL (-redirect)
// as imput arguments.
clientId = flag.String("id", "", "Client ID")
clientSecret = flag.String("secret", "", "Client Secret")
redirectURL = flag.String("redirect", "http://httpbin.org/get", "Redirect URL")
scopeURL = flag.String("scope", "https://www.googleapis.com/auth/userinfo.profile", "Scope URL")
authURL = "https://accounts.google.com/o/oauth2/auth"
tokenURL = "https://www.googleapis.com/oauth2/v1/userinfo"
apiBaseURL = "https://www.googleapis.com/"
)
const startInfo = `
Register new app at https://github.com/settings/applications and provide
-id, -secret and -redirect as input arguments.
`
func main() {
flag.Parse()
if *clientId == "" || *clientSecret == "" {
fmt.Println(startInfo)
flag.Usage()
os.Exit(2)
}
// Initialize service.
service := oauth2.Service(
*clientId, *clientSecret, authURL, tokenURL)
service.RedirectURL = *redirectURL
service.Scope = *scopeURL
// Get authorization url.
aUrl := service.GetAuthorizeURL("")
fmt.Println("\n", aUrl)
// Open authorization url in default system browser.
//webbrowser.Open(url)
fmt.Printf("\nVisit URL and provide code: ")
code := ""
// Read access code from cmd.
fmt.Scanf("%s", &code)
// Get access token.
token, _ := service.GetAccessToken(code)
fmt.Println()
// Prepare resource request.
google := oauth2.Request(apiBaseURL, token.AccessToken)
google.AccessTokenInHeader = false
google.AccessTokenInHeaderScheme = "token"
//google.AccessTokenInURL = true
// Make the request.
apiEndPoint := "user"
googleUserData, err := google.Get(apiEndPoint)
if err != nil {
log.Fatal("Get:", err)
}
defer googleUserData.Body.Close()
fmt.Println("User info response:")
// Write the response to standard output.
io.Copy(os.Stdout, googleUserData.Body)
fmt.Println()
}
以及緊急申請恐慌:
panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x2341]
這是有問題的行:
google := oauth2.Request(apiBaseURL, token.AccessToken)
我會很感激的幫助得到這個工作。請注意,該代碼只是gosimple/oauth2
回購協議中的修改示例代碼我試圖調試此階段,然後將其與控制器方法一起包裝到我的應用程序中。
完整的堆棧跟蹤如下:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2341]
goroutine 1 [running]:
main.main()
/Users/matt/Desktop/tests/google_demo.go:68 +0x341
goroutine 2 [syscall]:
goroutine 5 [runnable]:
net/http.(*persistConn).readLoop(0xc2000bd100)
/usr/local/Cellar/go/1.1/src/pkg/net/http/transport.go:761 +0x64b
created by net/http.(*Transport).dialConn
/usr/local/Cellar/go/1.1/src/pkg/net/http/transport.go:511 +0x574
goroutine 6 [select]:
net/http.(*persistConn).writeLoop(0xc2000bd100)
/usr/local/Cellar/go/1.1/src/pkg/net/http/transport.go:774 +0x26f
created by net/http.(*Transport).dialConn
/usr/local/Cellar/go/1.1/src/pkg/net/http/transport.go:512 +0x58b
...並迫使恐慌令牌上的錯誤:
panic: No access token found, response: [78 111 116 32 70 111 117 110 100]
代碼中的某些對象必須爲零。您能告訴我們恐慌發生的確切路線,併發布該行的副本,以便我們可以在代碼中找到它? –
@DavidGrayson第68行(它在帖子中,只是被埋沒了;忘了SO沒有行號) – elithrar
你在它之前寫了一個波浪號,所以我認爲它可能是一個近似的行號。所以根據你的編輯我會猜測'token'是零。我想知道爲什麼。 –