| 1 | // Copyright 2013 Gary Burd |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations |
| 13 | // under the License. |
| 14 | |
| 15 | package oauth_test |
| 16 | |
| 17 | import ( |
| 18 | "net/http" |
| 19 | "net/url" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/gomodule/oauth1/oauth" |
| 23 | ) |
| 24 | |
| 25 | // This example shows how to sign a request when the URL Opaque field is used. |
| 26 | // See the note at http://golang.org/pkg/net/url/#URL for information on the |
| 27 | // use of the URL Opaque field. |
| 28 | func ExampleClient_SetAuthorizationHeader(client *oauth.Client, credentials *oauth.Credentials) error { |
| 29 | form := url.Values{"maxResults": {"100"}} |
| 30 | |
| 31 | // The last element of path contains a "/". |
| 32 | path := "/document/encoding%2gizp" |
| 33 | |
| 34 | // Create the request with the temporary path "/". |
| 35 | req, err := http.NewRequest("GET", "http://api.example.com/", strings.NewReader(form.Encode())) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | // Overwrite the temporary path with the actual request path. |
| 41 | req.URL.Opaque = path |
| 42 | |
| 43 | // Sign the request. |
| 44 | if err := client.SetAuthorizationHeader(req.Header, credentials, "GET", req.URL, form); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 49 | |
| 50 | resp, err := http.DefaultClient.Do(req) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | defer resp.Body.Close() |
| 55 | // process the response |
| 56 | return nil |
| 57 | } |