title: Cannot get data from Google Analytics (401 Unauthorized)
body: <p>I try to implement basic data feed example  and all I get is just: </p>
  
  <blockquote>
    <p>com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized.</p>
  </blockquote>
  
  <p>But when I use <a href="http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html" rel="nofollow">http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html</a><br>
  It works fine with my Google Analytics account.</p>
  
  <p>It seems I've done everything according to OAuth2 instructions: created client id and client secret, enabled Analytcs API. </p>
  
  <p>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?</p>
  
  <p>Here is my code (of course I run this code with actual CLIENT_ID, CLIENT_SECRET and TABLE_ID):</p>
  
  <pre><code>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();
              }
  
      } 
  </code></pre>
  
  <p>Here is the stack trace:</p>
  
  <blockquote>
    <p>com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized<br>
    {<br>
     "code" : 401,<br>
     "errors" : [ {<br>
       "domain" : "global",<br>
       "location" : "Authorization",<br>
       "locationType" : "header",<br>
       "message" : "Invalid Credentials",<br>
       "reason" : "authError"<br>
     } ],<br>
     "message" : "Invalid Credentials"<br>
    }<br>
      at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:138)<br>
      at com.google.api.client.googleapis.services.GoogleClient.execute(GoogleClient.java:123)<br>
      at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:67)<br>
      at com.google.api.services.analytics.Analytics$Data$Ga$Get.execute(Analytics.java:1335)<br>
      at voc.AnalyticsTest.main(AnalyticsTest.java:76)</p>
  </blockquote>