NOTE: This page has been translated automatically from Russian to English. Original page.
Google Analytics API, Oauth2.0 и 1c8
The task - to get google analytics site from 1C: 8. And get this information regularly scheduled jobs, in order to analyze the sale, together with the traffic
For bundles with Google analytics API using OAUTH 2.0 protocol.
Articles that have helped me to get acquainted with the protocol:
https://developers.google.com/identity/protocols/OAuth2#basicsteps
https://habrahabr.ru/company/mailru/blog/115163/
In an ideal - it is necessary to use an HTTP connection and SSL, but copy-paste has never failed)
Step 1: Register your project in the developer console
https://console.developers.google.com/apis/library
I create a project. On the Overview tab, turn right api - Google analytics, on the Account tab data - create credentials, choose the item the OAuth client ID, the application type - other types. Result - oauth client, with ID and client secret that I use to access the google api.
Step 2: Get the response code.
I create processing, place the box on the form HTMLdokumenta named Browser. I connect the standby processor. On the action button place the procedure
Options = "response_type = code" + "&";
Options = Options + "client_id =" + client_id + "&";
Options = Options + "redirect_uri = http: // localhost" + "&";
Options = Options + "access_type = offline" + "&";
Options = Options + "scope = https: //www.googleapis.com/auth/analytics";
AdresAvtorizatsii = "https://accounts.google.com/o/oauth2/auth" + "?";
PolnyyAdresAvtorizatsii AdresAvtorizatsii + = parameters;
ElementyFormy.Brauzer.Pereyti (PolnyyAdresAvtorizatsii);
client_id - identifier created by the project
When you click Run - the user is prompted to enter your login / password in Google and allow the application to access the data. Answer ID look in the browser's title: ElementyFormy.Brauzer.dokument.URLUnencoded
Step 3. Redeem the code (code) to access key (access_token).
XTTPZapros = New COMObekt ( "WinHttp.WinHttpRequest.5.1");
Script = new COMObekt ( "MSScriptControl.ScriptControl");
Skript.language = "javascript";
Skript.AddObject ( "XTTPZapros" XTTPZapros);
Skript.Eval ( "XTTPZapros.Option (4) = 13056");
XTTPZapros.Open ( "Post", "https://accounts.google.com/o/oauth2/token", 0);
XTTPZapros.setRequestHeader ( "Content-Type", "application / x-www-form-urlencoded");
ParametryPOST = "grant_type = authorization_code" + "&";
ParametryPOST ParametryPOST = + "below code =" + KodDostupa + "&";
ParametryPOST ParametryPOST = + "client_id =" + client_id + "&";
ParametryPOST ParametryPOST = + "client_secret =" + client_secret + "&";
ParametryPOST ParametryPOST = + "redirect_uri = http: // localhost";
XTTPZapros.send (ParametryPOST);
The text response: { "access_token": "ya29.CjHUAjuyyO6y5HZB9zOC -R6SurYOg7zoUIM9YQASLrBBT_S6F6rNxkvSzV2SNyt3R ...", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "1 / Zq3Vi90TZ3O52ft2NDley9-OqO7FxlefusSHH ...."}
access_token help me get the right data api google, but expires_in parameter shows the time of life, after which the key is useless. In order to receive data every day without the participation of the user, stores the value refresh_token, which will be exchanged for access_token.
Step 4 (for the future). Exchange refresh_token on access_token
XTTPZapros = New COMObekt ( "WinHttp.WinHttpRequest.5.1");
Script = new COMObekt ( "MSScriptControl.ScriptControl");
Skript.language = "javascript";
Skript.AddObject ( "XTTPZapros" XTTPZapros);
Skript.Eval ( "XTTPZapros.Option (4) = 13056");
XTTPZapros.Open ( "Post", "https://accounts.google.com/o/oauth2/token", 0);
XTTPZapros.setRequestHeader ( "Content-Type", "application / x-www-form-urlencoded");
GoogleDannye = Spravochniki.Sayty.google;
ParametryPOST = "grant_type = refresh_token" + "&";
ParametryPOST ParametryPOST = + "client_id =" + client_id + "&";
ParametryPOST ParametryPOST = + "client_secret =" + client_secret + "&";
ParametryPOST ParametryPOST = + "refresh_token =" + refresh_token;
XTTPZapros.send (ParametryPOST);
TekstOtveta XTTPZapros.responsetext = ();
grant_type modified parameter and Set refresh_token
Answer this query looks like this:
{ "Access_token": "ya29.CjHUAlFwX3C196iBqH2UgBTi9qYu68zC6hTuWb9O0cvZIGUedwVIbTXc_Zm2yX8KK ..", "token_type": "Bearer", "expires_in": 3600}
Step 5: Get the data traffic.
Thus, access is gained. It remains to make a request - to request the data. Google has already taken care of this issue and has kindly given page https://ga-dev-tools.appspot.com/query-explorer/ , for what they are great thanks! As a result, the code for traffic looks like this:
HTTPZapros = New COMObekt ( "WinHttp.WinHttpRequest.5.1");
Script = new COMObekt ( "MSScriptControl.ScriptControl");
Skript.language = "javascript";
Skript.AddObject ( "HTTPZapros" HTTPZapros);
Skript.Eval ( "HTTPZapros.Option (4) = 13056");
DataZaprosa = Format (tekuschayaData () "DF = yyyy-MM-dd");
TekstApi = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A115479052&client_id="+ client_id +" & start-date = "+ DataZaprosa +" & end-date = "+ + DataZaprosa" & metrics = ga% 3Asessions ";
HTTPZapros.Open ( "GET", TekstApi, 0);
HTTPZapros.setRequestHeader ( "Authorization", "Bearer" + token);
HTTPZapros.Send ();
Result = HTTPZapros.ResponseText ();
Result - Json-file. If you have a platform above 8.3.6.1977, then process this file can be the platform means. Otherwise - the parsing of its own.
Step 6: Create a routine task, launching Step 4 and Step 5 and save on data traffic
This article is written for the first acquaintance with the Google API.