google-analytics
google-analytics copied to clipboard
Authenticating with service account in Python3
I am trying to run queries on my google analytics project. I use python3 and I authenticate using the p12 key associate to my service account. I ran into few issues and I was hoping to get some feedback.
After reading the wiki, I installed pycrypto and attempted to authenticate with:
ga.authenticate(client_email=client_email, private_key=private_key)
- parsing the p12 key didn't work. I solved that removing pycrypto and installing the cryptography library.
- assigning the value of
private_key
I stumbled into an encoding problem:
private_key=self.private_key.encode('utf-8'),
AttributeError: 'bytes' object has no attribute 'encode'
I solved it modifying this line to remove the encode('utf-8)
call.
And now I'm stuck. ga.authenticate
returns a googleanalytics.account
object, containing a single account, however I don't know how to get from there to the profile I need to run my queries. Any idea?
Thanks for the feedback, I'll have to revamp the service account authentication one of these days – many of the underlying libraries have changed since I wrote it, so it's not surprising that it doesn't work anymore.
As for getting to your profile from your account, that's no different from any other account object: account.webproperties['my_property'].profiles['my_profile']
. Alternatively you can add profile
and webproperty
arguments to ga.authenticate
. If you're not sure which properties are available, just do account.webproperties
and you should get a list.
Review the documentation at https://github.com/debrouwere/google-analytics/wiki/Authentication and let me know if anything is unclear.
Thanks for your reply. Your first suggestion seemed to do the trick for me.
On Fri, Feb 12, 2016 at 3:30 AM, Stijn Debrouwere [email protected] wrote:
Thanks for the feedback, I'll have to revamp the service account authentication one of these days – many of the underlying libraries have changed since I wrote it, so it's not surprising that it doesn't work anymore.
As for getting to your profile from your account, that's no different from any other account object: account.webproperties['my_property'].profiles['my_profile']. Alternatively you can add profile and webproperty arguments to ga.authenticate. If you're not sure which properties are available, just do account.webproperties and you should get a list.
Review the documentation at https://github.com/debrouwere/google-analytics/wiki/Authentication and let me know if anything is unclear.
— Reply to this email directly or view it on GitHub https://github.com/debrouwere/google-analytics/issues/27#issuecomment-183226669 .
I am unable to get this to work. None of the steps by @dtgnn worked for me.
Installed pycrypto
, pyOpenSSL
as well as cryptography
and always get CryptoUnavailableError: No crypto library available
.