Clojure library to access the Bing Ads reporting API.
For this example to work you need:
Notes on how these are generated are below.
(ns example
(:require [binj.reporting :as b]
[clj-time.core :as t]))
(def account-id 12345)
(def dev-token "123456789MAGIC42")
(def client-id "deadbeef-1234-5678-9009-424242424242")
(def grant (b/oauth-code-grant client-id :token-file "bing.edn"))
(def auth (b/authorization-data developer-token grant))
(def report-request (b/account-performance-report-request
"name"
[account-id]
[:time-period :account-id :account-name :account-number :spend]
:aggregation :daily
:time-period {:start (t/date-time 2018 3 6) :end (t/date-time 2018 3 6)}))
(def results (b/submit-and-download report-request auth))
({"TimePeriod" "2018-03-06", "AccountId" "12345", "AccountName" "uSwitch Some Account", "AccountNumber" "X9876543", "Spend" 1666.66})
If you're using a new client-id or developer token you'll need to generate a token file. This is best done from a repl:
$ lein repl
...
user=> (require '[binj.reporting :as b])
user=> (def client-id "deadbeef-1234-5678-9009-424242424242")
user=> (def grant (b/oauth-code-grant client-id))
user=> (b/authorization-url grant)
#object[java.net.URL 0xdeadbeef "https://login.live.com/oauth20_authorize.srf?scope=bingads.manage&response_type=code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf&client_id=deadbeef-1234-5678-9009-424242424242"]
Visit the link generated and sign in to your BingAds account then grant your app the requested permissions. Copy the URL of the page you're redirected to
; Using the same session as before
user=> (def resp-url "https://login.live.com/oauth20_desktop.srf?code=SomeOther-UUID-THAT-OAUT-HNeedsToWork&lc=1666")
user=> (b/request-access-refresh-tokens grant resp-url)
{:access-token "some huge string", :refresh-token "some other string"}
The output of the final command should be saved so it can be used as the token-file.
If you see errors like:
AdApiFaultDetail_Exception Invalid client data. Check the SOAP fault details for more information sun.reflect.NativeConstructorAccessorImpl.newInstance0 (NativeConstructorAccessorImpl.java:-2)
Then the API is swallowing SOAP errors. More verbose output can be gained by running:
(. System setProperty "com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump" "true")
request-id
no longer exposed)submit-and-download
now returns a sequence of mapsNote: With version 0.4.0 there are some significant breaking changes to the the way reports are generated. It now uses the built in async polling mechanism provided in the SDK, so it no longer exposes the request-id. The submit-and-download
now returns a sequence of maps representing the report data.
Authorisation and Report Definitions are the same, however the mechanism to download them is breaking. Please be aware when upgrading to this version.
Copyright © 2018, uSwitch.
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.