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

  • SEARCH
  • Home
  • 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 8015689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:19:22+00:00 2026-06-04T20:19:22+00:00

I tried many different things to access a google Account in order to read

  • 0

I tried many different things to access a google Account in order to read out profile-data, but it failed every time.

    Exception in thread "main" 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"
}

I tried to access via following code

@SuppressWarnings("deprecation")
public static GoogleAccessProtectedResource connect(String CLIENT_ID,String CLIENT_SECRET,String SCOPE,String CALLBACK_URL,HttpTransport TRANSPORT,JsonFactory JSON_FACTORY) throws IOException{
    // Generate the URL to which we will direct users
        String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID,
            CALLBACK_URL, SCOPE).build();
        System.out.println("Paste this url in your browser: " + authorizeUrl);

        // Wait for the authorization code
        System.out.println("Type the code you received here: ");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String authorizationCode = in.readLine();

        // Exchange for an access and refresh token
        GoogleAuthorizationCodeGrant authRequest = new GoogleAuthorizationCodeGrant(TRANSPORT,
            JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authorizationCode, CALLBACK_URL);
        authRequest.useBasicAuthorization = false;
        AccessTokenResponse authResponse = authRequest.execute();
        String accessToken = authResponse.accessToken;
        GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken,
            TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken);
        HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
        System.out.println("Access token: " + authResponse.accessToken);

        // Make an authenticated request
        GenericUrl shortenEndpoint = new GenericUrl("https://www.googleapis.com/urlshortener/v1/url");
        String requestBody =
            "{\"longUrl\":\"http://farm6.static.flickr.com/5281/5686001474_e06f1587ff_o.jpg\"}";
        HttpRequest request = rf.buildPostRequest(shortenEndpoint,
            ByteArrayContent.fromString("application/json", requestBody));
        HttpResponse shortUrl = request.execute();
        BufferedReader output = new BufferedReader(new InputStreamReader(shortUrl.getContent()));
        System.out.println("Shorten Response: ");
        for (String line = output.readLine(); line != null; line = output.readLine()) {
          System.out.println(line);
        }

        // Refresh a token (SHOULD ONLY BE DONE WHEN ACCESS TOKEN EXPIRES)
        //access.refreshToken();
        //System.out.println("Original Token: " + accessToken + " New Token: " + access.getAccessToken());

        return access;


  }

Then i want to access my account

public static void getData1(String accessToken, String clientId, String clientSecret, String refreshToken) throws IOException{
    // Set up the HTTP transport and JSON factory
      HttpTransport httpTransport = new NetHttpTransport();
      JsonFactory jsonFactory = new JacksonFactory();

      // Set up OAuth 2.0 access of protected resources
      // using the refresh and access tokens, automatically
      // refreshing the access token when it expires
      GoogleAccessProtectedResource requestInitializer =
          new GoogleAccessProtectedResource(accessToken, httpTransport,
          jsonFactory, clientId, clientSecret, refreshToken);

      // Set up the main Google+ class
      Plus plus = Plus.builder(httpTransport, jsonFactory)
          .setHttpRequestInitializer(requestInitializer)
          .build();

      // Make a request to access your profile and display it to console
      Person profile = plus.people().get("[myID]").execute();
      System.out.println("ID: " + profile.getId());
      System.out.println("Name: " + profile.getDisplayName());
      System.out.println("Image URL: " + profile.getImage().getUrl());
      System.out.println("Profile URL: " + profile.getUrl());
  }

Following main method should work afterwards: CLIENT_ID and CLIENT_SECRET are created

public static void main(String[] args) throws IOException {
  GoogleAccessProtectedResource access1=Connection.connect(CLIENT_ID, CLIENT_SECRET, SCOPE, CALLBACK_URL, TRANSPORT, JSON_FACTORY);

  //refresh
  String token= access1.getAccessToken();
  access1.refreshToken();
  String refreshed=access1.getAccessToken(); 
//get data
    Connection.getData1(token, CLIENT_ID, CLIENT_SECRET, refreshed); 
}

This is my Data…

private static final String SCOPE = "https://www.googleapis.com/auth/urlshortener";
private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
private static final HttpTransport TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();

// FILL THESE IN WITH YOUR VALUES FROM THE API CONSOLE
private static final String CLIENT_ID = "[myID].apps.googleusercontent.com";
private static final String CLIENT_SECRET = "[myID]";

Now there is a bad Request Exception thrown

Exception in thread "main" com.google.api.client.http.HttpResponseException: 400 Bad  Request
{
 "error" : "invalid_client"
}
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:900)
at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.executeUnparsed(AccessTokenRequest.java:472)
at com.google.api.client.auth.oauth2.draft10.AccessTokenRequest.execute(AccessTokenRequest.java:486)
at Connection.connect(Connection.java:78)
at Connection.main(Connection.java:50)
  • 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-06-04T20:19:23+00:00Added an answer on June 4, 2026 at 8:19 pm

    This is hopefully pretty easy to fix.

    You’re using a SCOPE string of:

    https://www.googleapis.com/auth/urlshortener

    This is the scope for the URL Shortener (goo.gl) API, not the Google+ APIs. Instead you should probably try using the profile scope for Google+

    https://www.googleapis.com/auth/plus.me

    Documentation here:
    https://developers.google.com/+/api/oauth

    Above, it looks like you’re using [myID] to reference two different types of ID:

    In the below statement, the myID should be your Google+ profile ID (copied/pasted from the URL for your Google+ profile or instead use the string ‘me’ to represent the currently authorized user):

    Person profile = plus.people().get("[myID]").execute();

    However, for the CLIENT_ID, you should be using the values from your project in code.google.com/apis/console.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I use SplitLayoutPanel with UIBinder? I've tried so many different things but
I've tried many different things, but I cant seem to get rid of this
I've tried many different things but none is working. I have a picture (let
I have tried 2 different things with hibernate many to many relationship, but i
I tried many things but I always get cannot convert string to membershipuser from
I've tried many functions already, but I simply can't figure this out. The right
tried this many times but I just can't figure out how to define custom
I've tried so many different things. The point I'm at right now is this:
I have done all kinds of research and tried many different things. I know
This is probably a simple one, but I've tried loads of different things and

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.