我正在使用gosimple/oauth2包来处理用户的 OAuth2 登录。我对GitHub 示例代码的问题为零,它完美地工作并返回我想要的。
但是,我确实在让它与谷歌合作时遇到了问题。我已经正确定义了我的范围(据我所见),并且我得到了一个令牌响应,但是一旦我输入它,应用程序就会在第 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]