简体   繁体   中英

How to get the email from https://www.googleapis.com/plus/v1/people/me call using a google+ token?

i want to get also the email from https://www.googleapis.com/plus/v1/people/me call. I receive a lots of informations, but not the email. Can anyone help?

HttpGet request = new HttpGet("https://www.googleapis.com/plus/v1/people/me?scope=https://www.googleapis.com/auth/userinfo.email");
request.setHeader("Authorization", "Bearer <access_token>");
HttpResponse response = client.execute(request);

EDIT: Updated as part of the improved Google+ Sign-In options in December 2013 - you now can get the email address with the Google+ profile response.

You will get the email from this endpoint as long as you have the email or https://www.googleapis.com/auth/plus.profile.emails.read scopes. See https://developers.google.com/+/web/people/#retrieve_an_authenticated_users_email_address for details.

var request = gapi.client.plus.people.get( {'userId' : 'me'} );
request.execute(function(person) {
  if(person['emails']) {
    console.log(person['emails'][0].value);
  }
});

I found the answer. My aim was also this. And We can do it successfully :)

U need:

  1. https://www.googleapis.com/auth/userinfo.email consent

  2. accessToken

and after above two just make HTTP GET request to https://www.googleapis.com/oauth2/v2/userinfo

BUT with added header "Authorization: Bearer <accessToken> "

U can do it by many ways. My way is customised REST call

package com.google.plus.samples.photohunt.custom;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetClientGet {

// http://localhost:8080/RESTfulExample/json/product/get
public static void makeRequest(String access_token) {

  try {

    URL url = new URL("https://www.googleapis.com/oauth2/v2/userinfo");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Bearer "+access_token);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

    String output;
    System.out.println("REST call made. Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

  } catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM