使用github.com/mrjones/oauth连接E-Trade沙箱遇HTTP 400错误求助
连接E-Trade沙箱环境时持续收到HTTP 400 Bad Request错误
连接E-Trade沙箱环境时,一直碰到HTTP 400 Bad Request错误,换了好几个OAuth库都没能解决。但用Postman可以正常获取Token,说明我的凭据是没问题的。
错误信息
2023/06/25 12:41:27 getBody: httpExecute: HTTP response is not 200/OK as expected. Actual response: Response Status: '400 400' Response Code: 400 Response Body: <!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad Request</title><style type="text/css">h1 ... The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).</p><hr class="line" /><h3></h3></body></html> Request Headers: [key: Authorization, val: OAuth oauth_callback="http%3A%2F%2Flocalhost%3A8081%2Fcallback",oauth_consumer_key="fb...1d2884",oauth_nonce="8576997036700485716",oauth_signature="91oNjhRVVD...Ctz0SH88%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1687689686",oauth_version="1.0"]
代码实现
package main import ( "fmt" "log" "net/http" "github.com/gorilla/sessions" "github.com/mrjones/oauth" ) const ( consumerKey = "fb746...1d2884" consumerSecret = "1c1c1...78c8a8f992a9" callbackURL = "http://localhost:8081/callback" ) var ( sessionStore *sessions.CookieStore oauthClient *oauth.Consumer ) func main() { sessionStore = sessions.NewCookieStore([]byte("your_session_key")) oauthClient = oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "https://apisb.etrade.com/oauth/request_token", AuthorizeTokenUrl: "https://us.etrade.com/e/t/etws/authorize", AccessTokenUrl: "https://etws.etrade.com/oauth/access_token", }, ) http.HandleFunc("/login", handleLogin) http.HandleFunc("/callback", handleCallback) http.HandleFunc("/reauthenticate", handleReauthenticate) log.Println("Server started at http://localhost:8081") log.Fatal(http.ListenAndServe(":8081", nil)) } func handleLogin(w http.ResponseWriter, r *http.Request) { requestToken, requestURL, err := oauthClient.GetRequestTokenAndUrl(callbackURL) if err != nil { log.Print(err) http.Error(w, "Failed to get request token", http.StatusInternalServerError) return } session, _ := sessionStore.Get(r, "session-name") session.Values["request_token"] = requestToken session.Save(r, w) http.Redirect(w, r, requestURL, http.StatusFound) } func handleCallback(w http.ResponseWriter, r *http.Request) { session, _ := sessionStore.Get(r, "session-name") requestToken := session.Values["request_token"].(*oauth.RequestToken) accessToken, err := oauthClient.AuthorizeToken(requestToken, r.FormValue("oauth_verifier")) if err != nil { http.Error(w, "Failed to authorize token", http.StatusInternalServerError) return } session.Values["access_token"] = accessToken session.Save(r, w) fmt.Fprintf(w, "Authorization successful!") }
有没有开发者在连接E-Trade沙箱平台时遇到过类似问题?
内容的提问来源于stack exchange,提问作者Mike3355
相关产品推荐
相关产品推荐

