kotmyrevich / analytics-issues

Automatically exported from code.google.com/p/analytics-issues
0 stars 0 forks source link

Server returned HTTP response code: 401 for URL: https://www.googleapis.com/analytics/v3/management/accounts/XXXX/webproperties/XXXX/profiles/XXXX/unsampledReports?Bearer=XXXX #783

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

We are premium user for downloading/generating Unsampled report, We are able to 
generate report using google console and download the same using console.
Our requirement is to automate the process, so we are using java and google 
analytics management API to achieve it.
We are a valid service id and OUAuth 2.0 token with us, Using this we are able 
to download the unsampled reports that has been created manually using console.

Now we are trying to generate a report using the following API URL
POST 
https://www.googleapis.com/analytics/v3/management/accounts/XXXX/webproperties/X
XXX/profiles/XXXX/unsampledReports

To authorize the above, I have appended the token to the above url as below
https://www.googleapis.com/analytics/v3/management/accounts/XXXX/webproperties/X
XXX/profiles/XXXX/unsampledReports?Bearer=XXXX.

But it's failing with Server returned HTTP response code: 401  which is of 
invalid token as per this 
link(https://developers.google.com/analytics/devguides/config/mgmt/v2/mgmtFeedRe
ference)

We are using java to make the POST Request as below.
Please find the attachment for code.

Please help us resolving this issue.

Original issue reported on code.google.com by prasantv...@gmail.com on 5 Nov 2015 at 8:49

Attachments:

GoogleCodeExporter commented 9 years ago
Take a look at your authorization code. Feel free to ask for help on 
stackoverflow with the tags google-analytics and google-analytics-api.

Is there a reason you are not using Java Client libraries, they truly make 
authentication much easier.
https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/s
ervice-java

Original comment by mcoh...@google.com on 6 Nov 2015 at 12:08

GoogleCodeExporter commented 9 years ago
Due to security reason our firewall blocks the URL googleapi.com , due to this 
reason we are using proxy urls, which is not working in case google client 
library and failing with SSL exception/unknown host exception every time.

So API is the only option available with us and there is no problem 
authorization code as the same code works while downloading a report.
The only difference is GET and POST. while downloading the report we are using 
GET and while generating the report POST being used. If it is working for you, 
can you please share sample code fro POST request while generating the report.
(I already have attached my post request, I might be doing some mistake while 
preparing the data, Please validate the same)
It will be of great help for us.

Thanks 
Prasant

Original comment by prasantv...@gmail.com on 6 Nov 2015 at 1:27

GoogleCodeExporter commented 9 years ago
I have never seen the token passed in as a url parameter call bearer only ever 
access_token=ya29.....
you might try
 conn.setRequestProperty ("Authorization", "Bearer ya29......");

I would also play around with OAuth2 playground: goo.gl/t4QLLJ to get a sense 
of how the auth structure is expected.

An invalid token could mean simply that the token has expired.
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=

Original comment by mcoh...@google.com on 6 Nov 2015 at 5:40

GoogleCodeExporter commented 9 years ago
Thanks a lot for your valuable solution, It worked like a charm. I have tried 
my request in the OUAUTH playground and rectified my request accordingly.
The report got generated successfully, However I am facing another issue while 
downloading the same report, Kindly advise on the below issue.

We are using service id + access token to generate the report and the report is 
getting saved in cloud storage. Using the same token I am able to List all the 
properties of the report including title,bucket id and the name of the csv file.
Even if I am using the same token, It fails with the following exception

Server returned HTTP response code: 403 for URL: 
https://www.googleapis.com/storage/v1/b/6745420671024221519-opus-test/o/Prasant_
Report_Final_New_13th18.csv

The GET request is attached here.

Note: The same code is working fine if the report got generated using my userid 
instead of service id with help of manual work around. (The screen shots 
attached).
which is not possible to do this manual work around in case of service id. And 
also our requirement is to automate it instead of any kind of manual 
interaction.

Kindly help us to resolve this issue.

Regards
Prasant 

Regards

Original comment by prasantv...@gmail.com on 7 Nov 2015 at 6:06

Attachments:

GoogleCodeExporter commented 9 years ago
I think it is a user permissions issue. With the service account you are able 
to create unsampled reports and list them with the Google Analytics API. But 
you are unable to download the report with the same service account because 
when you initially configured the unsampled report you have it sent to a 
particular user account.

You could be getting this error for two reasons.
1. You have granted the service account access to your Google Analytics Account 
but not your Cloud Storage Account.
2. You have generated the authorization token with the correct API scopes for 
the Google Analytics API but not for the Cloud Storage API scopes.

I would strongly encourage you to include the FULL JSON response for your 
errors as wells any code you are using the authorize the API at stack overflow 
under the appropriate tags:
http://stackoverflow.com/questions/tagged/google-analytics
http://stackoverflow.com/questions/tagged/google-analytics-api
http://stackoverflow.com/questions/tagged/google-cloud-storage

Original comment by mcoh...@google.com on 9 Nov 2015 at 3:02

GoogleCodeExporter commented 9 years ago
Thanks a lot for your response, You are right, I was not using proper scope
for cloud. I got it resolved yesterday.
All are working fine now.

Original comment by prasantv...@gmail.com on 9 Nov 2015 at 6:26