// TODO(mpl): same in twitter. refactor. Except for the additional perms in AuthorizationURL call. func (imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error { oauthClient, err := ctx.NewOAuthClient(oAuthURIs) if err != nil { err = fmt.Errorf("error getting OAuth client: %v", err) httputil.ServeError(w, r, err) return err } tempCred, err := oauthClient.RequestTemporaryCredentials(ctx.HTTPClient(), ctx.CallbackURL(), nil) if err != nil { err = fmt.Errorf("Error getting temp cred: %v", err) httputil.ServeError(w, r, err) return err } if err := ctx.AccountNode.SetAttrs( importer.AcctAttrTempToken, tempCred.Token, importer.AcctAttrTempSecret, tempCred.Secret, ); err != nil { err = fmt.Errorf("Error saving temp creds: %v", err) httputil.ServeError(w, r, err) return err } authURL := oauthClient.AuthorizationURL(tempCred, url.Values{"perms": {"read"}}) http.Redirect(w, r, authURL, http.StatusFound) return nil }
func (im extendedOAuth2) ServeCallback(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) { if im.getUserInfo == nil { panic("No getUserInfo is provided, don't use the default ServeCallback!") } oauthConfig, err := im.auth(ctx) if err != nil { httputil.ServeError(w, r, fmt.Errorf("Error getting oauth config: %v", err)) return } if r.Method != "GET" { http.Error(w, "Expected a GET", 400) return } code := r.FormValue("code") if code == "" { http.Error(w, "Expected a code", 400) return } // picago calls take an *http.Client, so we need to provide one which already // has a transport set up correctly wrt to authentication. In particular, it // needs to have the access token that is obtained during Exchange. transport := &oauth.Transport{ Config: oauthConfig, Transport: notOAuthTransport(ctx.HTTPClient()), } token, err := transport.Exchange(code) log.Printf("Token = %#v, error %v", token, err) if err != nil { log.Printf("Token Exchange error: %v", err) httputil.ServeError(w, r, fmt.Errorf("token exchange error: %v", err)) return } picagoCtx := ctx.Context.New(context.WithHTTPClient(transport.Client())) defer picagoCtx.Cancel() userInfo, err := im.getUserInfo(picagoCtx) if err != nil { log.Printf("Couldn't get username: %v", err) httputil.ServeError(w, r, fmt.Errorf("can't get username: %v", err)) return } if err := ctx.AccountNode.SetAttrs( importer.AcctAttrUserID, userInfo.ID, importer.AcctAttrGivenName, userInfo.FirstName, importer.AcctAttrFamilyName, userInfo.LastName, acctAttrOAuthToken, encodeToken(token), ); err != nil { httputil.ServeError(w, r, fmt.Errorf("Error setting attribute: %v", err)) return } http.Redirect(w, r, ctx.AccountURL(), http.StatusFound) }
func (im *imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error { cred, err := auth(ctx) if err != nil { err = fmt.Errorf("Error getting API credentials: %v", err) httputil.ServeError(w, r, err) return err } oauthClient.Credentials = *cred tempCred, err := oauthClient.RequestTemporaryCredentials(ctx.HTTPClient(), ctx.CallbackURL(), nil) if err != nil { err = fmt.Errorf("Error getting temp cred: %v", err) httputil.ServeError(w, r, err) } im.setCreds(tempCred) authURL := oauthClient.AuthorizationURL(tempCred, nil) http.Redirect(w, r, authURL, 302) return nil }