Getting data from Google Analytics within a Magic program XPA 3.3g


Graham White
 

Hi,

 

Has anyone successfully read data from the Google Analytics API’s from within a Magic program?  If so can I contact you to discuss how please.

 

Regards

 

Graham White
Software Developer
Graham.White@...
EC Credit Control |  eccreditcontrol.com

 

IMPORTANT NOTICE: This e-mail message and any attachments are confidential to EC Credit Control and subject to legal privilege (which is not waived or 

lost by mistaken delivery). If you have received this e-mail in error, please advise the sender immediately and destroy the message and any attachments.  

If you are not the intended recipient you are notified that any use, distribution, amendment, copying or any action taken or omitted to be taken in reliance of 

this message or attachments is prohibited. EC Credit Control collects personal information to provide and market our services (see our privacy policy at: 

www.eccreditcontrol.co.nz – www.eccreditcontrol.com - www.eccreditcontrol.com.au for more information about use, disclosure and access). 

EC Credit Control’s liability in connection with transmitting, unauthorised access to, or viruses in this message and its attachments is limited to resupply of 

any affected message or attachments.


Friedrich Geisler-Buckert
 

Hi Graham,

i never worked with this API. What is the problem?

It looks like a 'normal REST API' you can query with xpa. I did something similar with Elasticserach/xpa, which uses a REST API.

Best Regards 

Friedrich 


Andreas Sedlmeier
 

Integrating Google Analytics with Elasticsearch to access from Magic XPA is maybe a good idea anyways. Should help a lot.


Friedrich Geisler-Buckert
 

@Andreas: Absolutely - but this was just a Elasticsearch topic with a lot of REST APIs and JSON (...and XML..)


Andreas Sedlmeier
 

And now, since you did that exercise you can use Elasticsearch for quite everything and have Kibana for nice visualizations and dashboards. Here's an example of a logstash plugin for Google Analytics. https://github.com/russorat/logstash-input-googleanalytics . JSON with Magic is still a pain I guess or did you do it with .NET ?


Friedrich Geisler-Buckert
 

Yes, of course. I used the https://www.newtonsoft.com/json to put JSON -> XML. Than you can use xpa functions again.
BTW: Searching with Elasticsearch is incredible fast - i never expected this. But it needs a little time to build the index.


Graham White
 

Hi Friedrich,

My initial problem is obtaining a token from Google for a service account.  I have to create a JWT Base64url encoded.  Below is the example they give at Using OAuth 2.0 for Server to Server Applications  |  Google Identity  

This is what it says at that link above

The signing algorithm in the JWT header must be used when computing the signature. The only signing algorithm supported by the Google OAuth 2.0 Authorization Server is RSA using SHA-256 hashing algorithm. This is expressed as RS256 in the alg field in the JWT header.

Sign the UTF-8 representation of the input using SHA256withRSA (also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function) with the private key obtained from the Google API Console. The output will be a byte array.

Below is an example of a JWT before Base64url encoding:
{"alg":"RS256","typ":"JWT"}.
{
"iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@...",
"scope":"https://www.googleapis.com/auth/prediction",
"aud":"https://oauth2.googleapis.com/token",
"exp":1328554385,
"iat":1328550785
}.
[signature bytes]

Based on that I have created 

{"alg":"RS256","typ":"JWT"}.
{
"iss":"EMAIL ADDRESS WE GOT",
"scope":"https://www.googleapis.com/auth/analytics.readonly",
"aud":"https://oauth2.googleapis.com/token",
"exp":1622108446,
"iat":1622104846
}.
{"OUR PRIVATE KEY"}

I then base64url encode it and use that in curl to get a token.  No matter what I do I always get

"error": "invalid_request",
  "error_description": "Bad Request"

I am a bit of a loss to figure out why.

I am unsure of exactly this means "Sign the UTF-8 representation of the input using SHA256withRSA (also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function) with the private key obtained from the Google API Console. The output will be a byte array."

Do I need to do something with the private key we received when registering the service account?  What am i missing?

I hope you or someone can help.

Regards
Graham White



Friedrich Geisler-Buckert
 

Hi Graham,

sounds complex - this not realy my skill.

I saw under https://jwt.io/ is a debbuger. Did you try to work with this?

Best regards
Friedrich


Andreas Sedlmeier
 

On Wed, May 26, 2021 at 11:00 PM, Graham White wrote:
Do I need to do something with the private key we received when registering the service account?
You need to do what the documentation tells, apply SHA256WithRSA to your message (that JSON). SHA256WithRSA gets the message and your private key as parameters. You then add the resulting byte string to the message and post that. You would not base64 encode it. 

A quick search for SHA256WithRSA implemented with .NET resulted in nothing. Java examples are easy to find.

Best regards,

Andreas


JK Heydt
 

Is this something you do in Magic with client-server programming? Sounds interesting but I don't see the connection. TIA