我正在使用 go 的 oauth2 包代表用户向 Instagram 发出请求。我唯一需要弄清楚的部分是如何存储访问/刷新令牌,然后如何在 oauth2 中再次使用它?这是我到目前为止的代码,它所做的只是获取访问令牌并向 API 发出一个请求。之后我不知道该怎么办。
package main
import "net/http"
import "io/ioutil"
import "fmt"
import "html/template"
import "golang.org/x/oauth2"
var ClientID = YOUR_CLIENT_ID
var ClientSecret = YOUR_CLIENT_SECRET
var RedirectURI = "http://localhost:8080/redirect"
var authURL = "https://api.instagram.com/oauth/authorize"
var tokenURL = "https://api.instagram.com/oauth/access_token"
var templ = template.Must(template.New("index.html").ParseFiles("index.html"))
var igConf *oauth2.Config
func redirect(res http.ResponseWriter, req *http.Request) {
code := req.FormValue("code")
if len(code) != 0 {
tok, err := igConf.Exchange(oauth2.NoContext, code)
if err != nil {
fmt.Println(err)
http.NotFound(res, req)
return
}
if …Run Code Online (Sandbox Code Playgroud)