Your code snippet can only be part of what you did, but it seems that many of the OAUTH steps Google needed to connect were missing.
You can get more information on what these steps are by looking at this URL: https://developers.google.com/accounts/docs/OAuth2WebServer#offline
In the remainder of this answer, I described my own experience by doing something similar (loading data from GA and loading it into the database using SQL statements).
Start by getting customerKey and consumerSecret for your Google project. You will need to have a URL to which Google can redirect, both when requesting your Consumer Key and to supply OAuth to Google during your calls. They must match.
The next step is to send a GET request to Google. Here is an example in C # that can be built using SQL string concatenation.
String URL_AUTH_FIRST = "https://accounts.google.com/o/oauth2/auth"; String URL_TOKEN_ENDPOINT_SECOND = "https://accounts.google.com/o/oauth2/token"; String url = String.Format( "{0}?client_id={1}&redirect_uri={2}&access_type=offline&scope={3}&response_type=code&state={4}&approval_prompt=force", URL_AUTH_FIRST,_consumerKey_web_app,redir_url,scope,state);
To do this, you will need a built-in browser. Google.com will redirect this browser to a site under their control so that the user can log in or refuse authorization of your application. After Google receives the necessary information, they redirect back to your built-in browser. You can follow some steps using copy / paste in your browser, but at some point (described below) you should POST return some data that I donβt know if you can do this from the browser application.
Google will respond by redirecting the embedded browser to the URL. The url has data. You need to analyze the parameters of the URL and find the parameter "code". If you get a URL with a βcodeβ as a parameter, you must send the POST format in a specific format back to Google.
WebClient client = get_WebClient(); // proprietary to include things like proxy info try { NameValueCollection values = new NameValueCollection(); values.Add("client_id", _consumerKey_web_app); values.Add("client_secret", _consumerSecret_web_app_offline); values.Add("grant_type", "authorization_code"); values.Add("redirect_uri", URL_GOOGLE_REDIRECTS_TO_THIS_URL_AFTER_URL_AUTH); values.Add("code", authorization_code); Byte[] responseBytes = client.UploadValues(URL_TOKEN_ENDPOINT_SECOND, values); }
Google will return "responseBytes", which will be a formatted json string resembling:
{ "access_token":"1/fFAGRNJru1FTz70BzhT3Zg", "expires_in":3920, "token_type":"Bearer", "refresh_token":"1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ" }
Access_token is added to your REST API calls.
You can provide a GA request through the REST API, return the data and load it into your database using SQL statements. This is what my application does.
You can save this refresh_token and provide it in future connections. Indeed, all this sequence must be performed using a browser or browser interactively when a user logs on to the system. After that, and you got refresh_token, then your SQL can use and reuse refresh_token basically definitely, at least until the user password changes.
Google will also return error 401 on a regular basis. This means that you need to re-request the access token by sending a new set of values ββto Google:
NameValueCollection values = new NameValueCollection(); values.Add("client_id", _consumerKey_web_app); values.Add("client_secret", _consumerSecret_web_app_offline); values.Add("refresh_token", refresh_token); values.Add("grant_type", "refresh_token"); Byte[] responseBytes = client.UploadValues(URL_TOKEN_ENDPOINT_SECOND, values);