This project shows that how Google Adsense API works with Golang.
$ go get -u github.com/odg0318/google-adsense-example
- Make sure
Google Adsense API
is activated on Google API Console. - Create
OAuth2 Client ID
on Google API Console and setredirect_uri
tohttp://127.0.0.1:8080/auth
cd $GOPATH/github.com/odg0318/google-adsense-example
- Set following environments;
GOOGLE_CLIENT_ID
,GOOGLE_CLIENT_SECRET
,GOOGLE_REDIRECT_URI
- Execute
go build && ./google-adsense-example
and a web server will run. - Connect to http://127.0.0.1:8080/ on web browser and select your account to access.
- Some result of API will be shown on web browser.
Environment | Description |
---|---|
GOOGLE_CLIENT_ID | Google OAuth2 Client ID |
GOOGLE_CLIENT_SECRET | Google OAuth2 Client Secret |
GOOGLE_REDIRECT_URI | Redirect URI that is used to receive an authorization code from Google. |
You can check available API list here.
client := oauth2Conf.Client(oauth2.NoContext, tok)
service, err := adsense.New(client)
call := service.Accounts.List()
resp, err := call.Do()
client := oauth2Conf.Client(oauth2.NoContext, tok)
service, err := adsense.New(client)
call := service.Accounts.Reports()
call := report.Generate(accountId, fromDate, toDate)
call.Dimension("COUNTRY_CODE","DATE")
call.Metric("EARNINGS")
resp, err := call.Do()
Refresh Token is described in documentation.
url := oauth2Conf.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.ApprovalForce)