3

I'm trying to get a Google+ user's info using

https://www.googleapis.com/oauth2/v1/userinfo?alt=json

I pass the access token I previously obtained:

URL googleURL = new URL("https://www.googleapis.com/oauth2/v1/userinfo?alt=json");

            URLConnection googleConn = googleURL.openConnection();
            googleConn.setAllowUserInteraction(false);   
            googleConn.setDoOutput(true);

            **googleConn.setRequestProperty("Authorization", "Bearer "+accessToken);**

            BufferedReader rd = new BufferedReader(new InputStreamReader(googleConn.getInputStream()));

            String result = "";
            while(rd.ready())
                result += rd.readLine();
            rd.close();

But it gives me the HTTP error code 401 (invalid headers).

Is this the right way to pass the access token in the header?

googleConn.setRequestProperty("Authorization", "Bearer "+accessToken);

1 Answer 1

1

To access Google+ profile information you actually want to use the people get API: https://www.googleapis.com/plus/v1/people/me You may want to check out the reference docs or play with it in the API Explorer.

Also, you're coding at a much lower level than you need to. While you can use the REST endpoint directly, things will be a lot easier if you use the official client library. If you go this route, there's even a Google+ starter project for Java.

If you'd like to continue low level you should pass the access token in with OAuth prefix instead of Bearer. Your resulting HTTP request headers will look something like this:

GET https://www.googleapis.com/plus/v1/people/102817283354809142195

Authorization:  OAuth ya29.BHES61Rxr4Cq-Elcfpvx_2oWC443fMOEQV9iS5-M7ZjJ6xk
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.