Created
May 11, 2019 22:51
-
-
Save tomcam/08460a1b0ed71b48cfd02be0cc3a6592 to your computer and use it in GitHub Desktop.
oauth2 problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Goal is to obtain user consent token: | |
// https://developer.ebay.com/api-docs/static/oauth-consent-request.html | |
import ( | |
"fmt" | |
"golang.org/x/oauth2" | |
"log" | |
"net/http" | |
"os" | |
) | |
const ( | |
fakeRandomString = "xyz" | |
sandboxClientId = "<valid sandbox client ID>" | |
sandboxClientSecret = "<valid sandbox secret>" | |
productionClientId = "<valid production client ID>" | |
productionClientSecret = "<valid production client secret>" | |
RuName = "<valid RUName>" | |
port = ":8000" | |
) | |
var endpoint = oauth2.Endpoint{ | |
AuthURL: "https://auth.ebay.com/oauth2/authorize", | |
TokenURL: "https://api.ebay.com/identity/v1/oauth2/token", | |
AuthStyle: oauth2.AuthStyleInParams, | |
} | |
var sandboxEndpoint = oauth2.Endpoint{ | |
AuthURL: "https://signin.sandbox.ebay.com/authorize", | |
TokenURL: "https://api.sandbox.ebay.com/identity/v1/oauth2/token", | |
AuthStyle: oauth2.AuthStyleInParams, | |
} | |
// Goal is to get user access token for this API call: | |
// https://developer.ebay.com/api-docs/buy/offer/resources/bidding/methods/getBidding | |
var config = &oauth2.Config{ | |
ClientID: sandboxClientId, | |
ClientSecret: sandboxClientSecret, | |
RedirectURL: RuName, | |
Scopes: []string{ | |
"https://api.ebay.com/oauth/api_scope/buy.offer.auction", | |
}, | |
Endpoint: sandboxEndpoint, | |
} | |
func main() { | |
// If not running under sudo, remind that | |
// it's required. | |
if os.Getenv("SUDO_USER") == "" { | |
fmt.Println("\nUsage: sudo " + os.Args[0] + " 2>>stderror.log\n") | |
log.Fatal("") | |
} | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "<h1>Please sign in at eBay</h1>") | |
u := config.AuthCodeURL(fakeRandomString) | |
//http.Redirect(w, r, u, http.StatusFound) // Or http.StatusSeeOther? | |
http.Redirect(w, r, u, http.StatusSeeOther) // Or http.? | |
}) | |
http.HandleFunc("/auth-accepted.html", func(w http.ResponseWriter, r *http.Request) { | |
code := r.FormValue("code") | |
if code != "" { | |
fmt.Fprintf(w, "<h1>Sign-in successful</h1>") | |
fmt.Fprintf(w, "<p>Token is %v</p>", code) | |
} else { | |
// I can actually see it in the address bar! | |
fmt.Fprintf(w, "<h1>Dude. Where's the token?</h1>") | |
fmt.Fprintf(os.Stdout, "Token: %v\n", code) | |
} | |
}) | |
http.HandleFunc("/auth-declined.html", func(w http.ResponseWriter, r *http.Request) { | |
http.ServeFile(w, r, "./auth-declined.html") | |
}) | |
http.HandleFunc("/privacy.html", func(w http.ResponseWriter, r *http.Request) { | |
http.ServeFile(w, r, "./privacy.html") | |
}) | |
log.Fatal(http.ListenAndServe(":80", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment