<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 使用OAuth2 OAuth2是一種用于API通信的相對常見的協議。golang.org/x/oauth2 包提供了非常靈活的OAuth2操作,它的子包能為Facebook,Google和GitHub等各種提供商提供支持。 本節將演示如何為GitHub的搭建OAuth2和一些基本操作。 ### 實踐 1. 獲取支持庫: ``` go get golang.org/x/oauth2 ``` [配置OAuth Client](https://github.com/settings/applications/new) 設置 Client ID and Secret: ``` 1. export GITHUB_CLIENT="your_client" 2. export GITHUB_SECRET="your_secret" ``` 可以在[https://developer.github.com/v3/](https://developer.github.com/v3/) 查看相關操作文檔。 2. 建立 config.go: ``` package oauthcli import ( "context" "fmt" "os" "golang.org/x/oauth2" "golang.org/x/oauth2/github" ) // 設置oauth2.Config // 需要在環境變量中設置GITHUB_CLIENT GITHUB_SECRET func Setup() *oauth2.Config { return &oauth2.Config{ ClientID: os.Getenv("GITHUB_CLIENT"), ClientSecret: os.Getenv("GITHUB_SECRET"), Scopes: []string{"repo", "user"}, Endpoint: github.Endpoint, } } // GetToken 檢索github oauth2令牌 func GetToken(ctx context.Context, conf *oauth2.Config) (*oauth2.Token, error) { url := conf.AuthCodeURL("state") fmt.Printf("Type the following url into your browser and follow the directions on screen: %v\n", url) fmt.Println("Paste the code returned in the redirect URL and hit Enter:") var code string if _, err := fmt.Scan(&code); err != nil { return nil, err } return conf.Exchange(ctx, code) } ``` 3. 建立 exec.go: ``` package oauthcli import ( "fmt" "io" "net/http" "os" ) // GetUsers 使用oauth2獲取用戶信息 func GetUsers(client *http.Client) error { url := fmt.Sprintf("https://api.github.com/user") resp, err := client.Get(url) if err != nil { return err } defer resp.Body.Close() fmt.Println("Status Code from", url, ":", resp.StatusCode) io.Copy(os.Stdout, resp.Body) return nil } ``` 4. 建立 main.go: ``` package main import ( "context" "github.com/agtorre/go-cookbook/chapter6/oauthcli" ) func main() { ctx := context.Background() conf := oauthcli.Setup() tok, err := oauthcli.GetToken(ctx, conf) if err != nil { panic(err) } client := conf.Client(ctx, tok) if err := oauthcli.GetUsers(client); err != nil { panic(err) } } ``` 5. 這會輸出: ``` Visit the URL for the auth dialog: https://github.com/login/oauth/authorize? access_type=offline&client_id= <your_id>&response_type=code&scope=repo+user&state=state Paste the code returned in the redirect URL and hit Enter: <your_code> Status Code from https://api.github.com/user: 200 {<json_payload>} ``` ### 說明 標準OAuth2流是基于重定向的,會以服務器重定向到你指定的端點作為結束。然后你所在的端負責獲取返回值并將其交換為令牌。示例通過允許我們使用諸如https://localhost 或https://a-domain-you-own 之類的URL并手動復制/粘貼代碼然后按Enter鍵來繞過該要求。交換令牌后,客戶端將根據需要自行刷新令牌。 重要的是要注意我們不以任何方式存儲令牌。 如果程序崩潰,則必須重新交換令牌。同樣要注意,除非刷新令牌過期,丟失或損壞,否則我們只需要顯式檢索一次令牌。配置客戶端后,它應該能夠為其授權的API執行所有典型的HTTP操作,并且具有適當的范圍。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看