examples_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. package oauth_test
  15. import (
  16. "github.com/garyburd/go-oauth/oauth"
  17. "net/http"
  18. "net/url"
  19. "strings"
  20. )
  21. // This example shows how to sign a request when the URL Opaque field is used.
  22. // See the note at http://golang.org/pkg/net/url/#URL for information on the
  23. // use of the URL Opaque field.
  24. func ExampleClient_SetAuthorizationHeader(client *oauth.Client, credentials *oauth.Credentials) error {
  25. form := url.Values{"maxResults": {"100"}}
  26. // The last element of path contains a "/".
  27. path := "/document/encoding%2gizp"
  28. // Create the request with the temporary path "/".
  29. req, err := http.NewRequest("GET", "http://api.example.com/", strings.NewReader(form.Encode()))
  30. if err != nil {
  31. return err
  32. }
  33. // Overwrite the temporary path with the actual request path.
  34. req.URL.Opaque = path
  35. // Sign the request.
  36. if err := client.SetAuthorizationHeader(req.Header, credentials, "GET", req.URL, form); err != nil {
  37. return err
  38. }
  39. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  40. resp, err := http.DefaultClient.Do(req)
  41. if err != nil {
  42. return err
  43. }
  44. defer resp.Body.Close()
  45. // process the response
  46. return nil
  47. }