Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6981903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:13:05+00:00 2026-05-27T18:13:05+00:00

I try to implement basic data feed example and all I get is just:

  • 0

I try to implement basic data feed example and all I get is just:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized.

But when I use http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
It works fine with my Google Analytics account.

It seems I’ve done everything according to OAuth2 instructions: created client id and client secret, enabled Analytcs API.

Also I’ve tried to get my profiles and accounts with Analytics Management API but I get the same error. What do I do wrong?

Here is my code (of course I run this code with actual CLIENT_ID, CLIENT_SECRET and TABLE_ID):

public class AnalyticsTest {

    private static final String CLIENT_ID = "MY_CLIENT_ID";
    private static final String CLIENT_SECRET = "MY_CLIENT_SECRET";
    private static final String REDIRECT_URL = "urn:ietf:wg:oauth:2.0:oob";
    private static final String APPLICATION_NAME = "test";
    private static final String SCOPE = "https://www.googleapis.com/auth/analytics";
// TRIED AS WELL private static final String SCOPE = "https://www.google.com/analytics/feeds";
    private static final String TABLE_ID = "MY_TABLE_ID";

    public static void main(String[] args) throws IOException {
            NetHttpTransport netHttpTransport = new NetHttpTransport();
            JacksonFactory jacksonFactory = new JacksonFactory();
            // Generate the URL to send the user to grant access.
            String authorizationUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID, REDIRECT_URL, SCOPE).build();

            // Direct user to the authorization URI.
            System.out.println("Go to the following link in your browser:");
            System.out.println(authorizationUrl);

            // Get authorization code from user.
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What is the authorization code?");
            String authorizationCode = null;
            authorizationCode = in.readLine();

            // Use the authorization code to get an access token and a refresh
            // token.
            AccessTokenResponse response = null;
            try {
                    response = new GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant(netHttpTransport, jacksonFactory, CLIENT_ID, CLIENT_SECRET, authorizationCode, REDIRECT_URL).execute();
            } catch (IOException ioe) {
                    ioe.printStackTrace();
            }

            // Use the access and refresh tokens to get a new
            // GoogleAccessProtectedResource.
            GoogleAccessProtectedResource googleAccessProtectedResource = new GoogleAccessProtectedResource(response.accessToken, netHttpTransport, jacksonFactory, CLIENT_ID, CLIENT_SECRET, response.refreshToken);

            Analytics analytics = Analytics
                            .builder(netHttpTransport, jacksonFactory)
                            .setHttpRequestInitializer(googleAccessProtectedResource)
                            .setApplicationName(APPLICATION_NAME).build();
            //System.out.println(analytics.management().accounts().list().execute());

            Get apiQuery = analytics.data().ga().get(TABLE_ID, // "ga:" + Profile
                            // Id.
                            "2011-09-01", // Start date.
                            "2011-12-23", // End date.
                            "ga:visits"); // Metrics.

            try {
                    GaData gaData = apiQuery.execute();
                    // Success. Do something cool!

            } catch (GoogleJsonResponseException e) {
                    // Catch API specific errors.
                    e.printStackTrace();

            } catch (IOException e) {
                    // Catch general parsing errors.
                    e.printStackTrace();
            }

    } 

Here is the stack trace:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
“code” : 401,
“errors” : [ {
“domain” : “global”,
“location” : “Authorization”,
“locationType” : “header”,
“message” : “Invalid Credentials”,
“reason” : “authError”
} ],
“message” : “Invalid Credentials”
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:138)
at com.google.api.client.googleapis.services.GoogleClient.execute(GoogleClient.java:123)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:67)
at com.google.api.services.analytics.Analytics$Data$Ga$Get.execute(Analytics.java:1335)
at voc.AnalyticsTest.main(AnalyticsTest.java:76)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:13:05+00:00Added an answer on May 27, 2026 at 6:13 pm

    Try changing your scope from this:

    private static final String SCOPE = "https://www.googleapis.com/auth/analytics";
    

    to this:

    private static final String SCOPE = "https://www.googleapis.com/auth/analytics.readonly";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I try to implement my class on this file I get an error
If I try to implement this simple-as-possible example: Update Content with AJAXRefreshRequest it does
My first try of MVC. Am trying to implement a simple example. Inspiration from
I try to implement a basic cookie helper in my application. Mainly I check
In attempting to implement fileUpload using the streaming API I get an error described
I'm trying to implement the Basic example for the GwtUpload library, as found here
I've try to implement integration tests for a working application (spring, hibernate, soap, cxf).
I had try to implement the send me log feature into my apps but
I would like to implement an B+ tree in Java and try to optimize
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.