0
Reddit擁有Oauth2的API端點,我需要使用正確的標題和數據執行POST以獲取訪問令牌。這裏是我的代碼:如何使用gorequest發出POST請求
package main
import (
"github.com/parnurzeal/gorequest"
"fmt"
)
func main() {
app_key := "K...A"
app_secret := "3...M"
ua_string := "script:bast:0.1 (by /u/a...h)"
username := "a...h"
password := "..."
r := gorequest.New().SetBasicAuth(app_key, app_secret).Set("User-Agent", ua_string)
resp, body, errs := r.Post("https://www.reddit.com/api/v1/access_token").Send(
map[string]string{
"grant_type": "password",
"username": username,
"password": password,
},
).End()
if errs != nil {
fmt.Println(errs)
}
fmt.Println(resp)
fmt.Println(resp.StatusCode)
fmt.Println(body)
}
但是它不工作,我得到:{"message": "Too Many Requests", "error": 429}
我不是在所有做太多的要求,我跟隨API規則了。
這是我的工作,其等效Python代碼:
import requests
import requests.auth
app_key = "K...A"
app_secret = "3...M"
ua_string = "script:bast:0.1 (by /u/a...h)"
username = "a...h"
password = "..."
client_auth = requests.auth.HTTPBasicAuth(app_key, app_secret)
post_data = {"grant_type": "password", "username": username,
"password": password}
headers = {"User-Agent": ua_string}
response = requests.post("https://www.reddit.com/api/v1/access_token",
auth=client_auth, data=post_data, headers=headers)
print(response.json())
所以,什麼是錯我的Go代碼?我在做什麼錯誤?
您是否必須使用「github.com/parnurzeal/gorequest」? 「net/http」的請求不好嗎? – Tanaike