Created
April 12, 2014 16:20
-
-
Save iandelahorne/10544018 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/goauth2/oauth" | |
urlshortener "code.google.com/p/google-api-go-client/urlshortener/v1" | |
"flag" | |
"fmt" | |
"log" | |
) | |
var ( | |
clientId = flag.String("id", "", "Client ID") | |
clientSecret = flag.String("secret", "", "Client Secret") | |
redirectURL = flag.String("redirect_url", "oob", "Redirect URL") | |
authURL = flag.String("auth_url", "https://accounts.google.com/o/oauth2/auth", "Authentication URL") | |
tokenURL = flag.String("token_url", "https://accounts.google.com/o/oauth2/token", "Token URL") | |
code = flag.String("code", "", "Authorization Code") | |
cachefile = flag.String("cache", "cache.json", "Token cache file") | |
url = flag.String("url", "http://www.vende.pe", "URL to shorten") | |
) | |
const usageMsg = ` | |
To obtain a request token you must specify both -id and -secret. | |
To obtain Client ID and Secret, see the "OAuth 2 Credentials" section under | |
the "API Access" tab on this page: https://code.google.com/apis/console/ | |
Once you have completed the OAuth flow, the credentials should be stored inside | |
the file specified by -cache and you may run without the -id and -secret flags. | |
` | |
func main() { | |
flag.Parse() | |
config := &oauth.Config{ | |
ClientId: *clientId, | |
ClientSecret: *clientSecret, | |
RedirectURL: *redirectURL, | |
Scope: urlshortener.UrlshortenerScope, | |
AuthURL: *authURL, | |
TokenURL: *tokenURL, | |
TokenCache: oauth.CacheFile(*cachefile), | |
} | |
transport := &oauth.Transport{Config: config} | |
token, err := config.TokenCache.Token() | |
if err != nil { | |
if *code == "" { | |
// Get an authorization code from the data provider. | |
// ("Please ask the user if I can access this resource.") | |
url := config.AuthCodeURL("") | |
fmt.Println("Visit this URL to get a code, then run again with -code=YOUR_CODE\n") | |
fmt.Println(url) | |
return | |
} | |
// Exchange the authorization code for an access token. | |
// ("Here's the code you gave the user, now give me a token!") | |
token, err = transport.Exchange(*code) | |
if err != nil { | |
log.Fatal("Exchange:", err) | |
} | |
// (The Exchange method will automatically cache the token.) | |
fmt.Printf("Token is cached in %v\n", config.TokenCache) | |
} | |
transport.Token = token | |
httpClient := transport.Client() | |
svc, err := urlshortener.New(httpClient) | |
shortUrl, err := svc.Url.Insert(&urlshortener.Url{ | |
Kind: "urlshortener#url", // Not really needed | |
LongUrl: *url, | |
}).Do() | |
fmt.Printf("The URL %s goes to %s\n", shortUrl.Id, *url) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment