英文:
gosimple/oauth2 and Google OAuth2
问题
我正在使用gosimple/oauth2包来处理用户的OAuth2登录。我对GitHub示例代码没有任何问题,它完美地工作并返回我想要的结果。
然而,我在与Google一起使用它时遇到了问题。我已经正确定义了我的范围(就我所看到的),并且我正在获取一个令牌响应,但是一旦我输入它,应用程序就会在大约第68行发生恐慌。
以下是引发恐慌的代码:
google := oauth2.Request(apiBaseURL, token.AccessToken)
我希望能得到帮助使其正常工作。请注意,这段代码只是我正在尝试调试的gosimple/oauth2
存储库中的修改示例代码的阶段,我在应用程序中的控制器方法中包装它之前。
英文:
I'm using the gosimple/oauth2 package to handle OAuth2 logins for users. I have zero problems with the GitHub example code, which works perfectly and returns what I want.
I do, however, have issues getting it to work with Google. I've defined my scope correctly (as far as I can see), and I'm getting a token response, but once I enter it in, the application panics at ~line 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()
}
And the panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2341]
This is the offending line:
google := oauth2.Request(apiBaseURL, token.AccessToken)
I'd appreciate help to get this working. Note that the code is just modified example code from the gosimple/oauth2
repo I'm trying to debug this stage before wrapping it with the controller methods in my application.
Full stack trace as below:
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
... and forcing a panic on a token error:
panic: No access token found, response: [78 111 116 32 70 111 117 110 100]
答案1
得分: 0
这是由于库的令牌处理中的一个错误导致的,我已经向作者记录了这个问题,并且在当天进行了修复:https://bitbucket.org/gosimple/oauth2/commits/e8e925ffb2424645cceaa9534e0ed9a28e564a20
英文:
This was due to a bug in the library's token handling, which I logged with the author and which was fixed within the day: https://bitbucket.org/gosimple/oauth2/commits/e8e925ffb2424645cceaa9534e0ed9a28e564a20
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论