我正在使用gosimple/oauth2包来处理用户的OAuth2登录。我对GitHub example code没有任何问题,它可以完美运行并返回我想要的内容。
但是,我确实遇到了与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
repo中的示例代码,我试图在我的应用程序中使用控制器方法包装它之前调试此阶段。
完整堆栈跟踪如下:
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]
答案 0 :(得分:0)
这是由于图书馆令牌处理中的一个错误,我与作者一起登录并在当天修复了:https://bitbucket.org/gosimple/oauth2/commits/e8e925ffb2424645cceaa9534e0ed9a28e564a20