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

给你一囗甜甜゛ 提交于 2020-01-15 23:18:59

问题


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);

回答1:


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);
  }
});



回答2:


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();

  }

}

}



来源:https://stackoverflow.com/questions/18124134/how-to-get-the-email-from-https-www-googleapis-com-plus-v1-people-me-call-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!