Skip to content

Instantly share code, notes, and snippets.

@mzimmerman
Created May 6, 2013 13:33
Show Gist options
  • Save mzimmerman/5525174 to your computer and use it in GitHub Desktop.
Save mzimmerman/5525174 to your computer and use it in GitHub Desktop.
Patch for goauth2 to enable usage of a Simple API Key
diff -r 2d711318934b oauth/oauth.go
--- a/oauth/oauth.go Tue Apr 30 13:15:44 2013 -0400
+++ b/oauth/oauth.go Mon May 06 09:20:21 2013 -0400
@@ -144,6 +144,7 @@
type Transport struct {
*Config
*Token
+ APIKey string
// Transport is the HTTP transport to use when making requests.
// It will default to http.DefaultTransport if nil.
@@ -227,8 +228,11 @@
// If the Token is invalid callers should expect HTTP-level errors,
// as indicated by the Response's StatusCode.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
- if t.Config == nil {
- return nil, OAuthError{"RoundTrip", "no Config supplied"}
+ if t.APIKey != "" && t.Config != nil {
+ return nil, OAuthError{"RoundTrip", "cannot use both an APIKey and Config"}
+ }
+ if t.Config == nil && t.APIKey == "" {
+ return nil, OAuthError{"RoundTrip", "no Config or APIKey supplied"}
}
if t.Token == nil {
if t.TokenCache == nil {
@@ -241,8 +245,8 @@
}
}
- // Refresh the Token if it has expired.
- if t.Expired() {
+ // Refresh the Token if it has expired
+ if t.Config != nil && t.Expired() {
if err := t.Refresh(); err != nil {
return nil, err
}
@@ -254,6 +258,15 @@
req = cloneRequest(req)
req.Header.Set("Authorization", "Bearer "+t.AccessToken)
+ // adding the "key" parameter onto the end of the request
+ // idea and code snippet stolen from noauth https://code.google.com/p/google-plus-go-starter/source/browse/noauth/noauth.go
+ if t.APIKey != "" {
+ u := req.URL
+ q := u.Query()
+ q.Add("key", t.APIKey)
+ u.RawQuery = q.Encode()
+ }
+
// Make the HTTP request.
return t.transport().RoundTrip(req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment