@@ -30,6 +30,7 @@ type OAuthFlow struct {
3030 ClientID string
3131 ClientSecret string
3232 WriteSuccessHTML func (io.Writer )
33+ VerboseStream io.Writer
3334}
3435
3536// ObtainAccessToken guides the user through the browser OAuth flow on GitHub
@@ -52,12 +53,14 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
5253 q .Set ("state" , state )
5354
5455 startURL := fmt .Sprintf ("https://%s/login/oauth/authorize?%s" , oa .Hostname , q .Encode ())
56+ oa .logf ("open %s\n " , startURL )
5557 if err := openInBrowser (startURL ); err != nil {
5658 fmt .Fprintf (os .Stderr , "error opening web browser: %s\n " , err )
5759 fmt .Fprintf (os .Stderr , "Please open the following URL manually:\n %s\n " , startURL )
5860 }
5961
6062 http .Serve (listener , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
63+ oa .logf ("server handler: %s\n " , r .URL .Path )
6164 if r .URL .Path != "/callback" {
6265 w .WriteHeader (404 )
6366 return
@@ -69,6 +72,7 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
6972 return
7073 }
7174 code = rq .Get ("code" )
75+ oa .logf ("server received code %q\n " , code )
7276 w .Header ().Add ("content-type" , "text/html" )
7377 if oa .WriteSuccessHTML != nil {
7478 oa .WriteSuccessHTML (w )
@@ -77,7 +81,9 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
7781 }
7882 }))
7983
80- resp , err := http .PostForm (fmt .Sprintf ("https://%s/login/oauth/access_token" , oa .Hostname ),
84+ tokenURL := fmt .Sprintf ("https://%s/login/oauth/access_token" , oa .Hostname )
85+ oa .logf ("POST %s\n " , tokenURL )
86+ resp , err := http .PostForm (tokenURL ,
8187 url.Values {
8288 "client_id" : {oa .ClientID },
8389 "client_secret" : {oa .ClientSecret },
@@ -109,6 +115,13 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
109115 return
110116}
111117
118+ func (oa * OAuthFlow ) logf (format string , args ... interface {}) {
119+ if oa .VerboseStream == nil {
120+ return
121+ }
122+ fmt .Fprintf (oa .VerboseStream , format , args ... )
123+ }
124+
112125func openInBrowser (url string ) error {
113126 var args []string
114127 switch runtime .GOOS {
0 commit comments