我正在从Android客户端收到token-id
在第一步中,我通过以下代码验证令牌:
$tokenId = $request->get('token-id');
$google_client = new Google_Client();
$google_client->setApplicationName("Ashojash");
$google_client->setClientId(env("GOOGLE_KEY"));
$google_client->setClientSecret(env("GOOGLE_SECRET"));
$google_client->setIncludeGrantedScopes(true);
$google_client->addScope(['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/plus.login']);
$credentials = $google_client->verifyIdToken($tokenId);
if ($credentials)
{
$data = $credentials->getAttributes();
// validate aud against server client_id
return $data['payload']['sub']; // user ID
}
return false;
从Google收到的数据是:
array:2 [
"envelope" => array:2 [
"alg" => "RS256"
"kid" => "23e5872762976b37944c33e4b2602656093ece91"
]
"payload" => array:12 [
"iss" => "https://accounts.google.com"
"aud" => "346904023124-73h70s03c0dipla8ltcbcd2ko076ft43.apps.googleusercontent.com"
"sub" => "111167217866315036918"
"email_verified" => true
"azp" => "346904023124-lehbs8510nibuq5ci125h8mu6u2ir4q1.apps.googleusercontent.com"
"email" => "jhon.f.kenedy777@gmail.com"
"iat" => 1452178925
"exp" => 1452182525
"name" => "john F.kenedy"
"given_name" => "john"
"family_name" => "F.kenedy"
"locale" => "en"]
]
我验证了令牌ID后,如何获取用户信息?
我感兴趣的是:
1,电子邮件
2 - 用户基本信息
3-Google plus info
我也安装了Socialite,所以如果可能的话也包括你在社交网站上的答案。
答案 0 :(得分:2)
使用google-api-php-client library,一旦您从$ google_client-> verifyIdToken获得结果,您就可以访问已在问题中发布的所有属性。
经过测试:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
require_once (ROOT_PATH.'/google-api-php-client/src/Google_Client.php');
$ClientID_debug = 'xxxxxxx.apps.googleusercontent.com';
$ClientID_release = 'xxxxxxx.apps.googleusercontent.com';
$mToken = $_POST['mToken'];
$build = $_POST['build'];
$google_client = new Google_Client();
if($build=='DEBUG'){
$google_client->setClientId($ClientID_debug);
}else{
$google_client->setClientId($ClientID_release);
}
$google_result = $google_client->verifyIdToken($mToken);
if ($google_result){
$data = $google_result->getAttributes();
$tokenUserID = $data['payload']['sub'];
$tokenAudience = $data['payload']['aud'];
$tokenEmail = $data['payload']['email'];
//etc..
//send result back to Android client (could also use JSON to send all attributes)
echo($tokenAudience);
}
使用AsyncTask从Android调用:
protected String doInBackground(String... params) {
try {
//Always use https to prevent man-in-the-middle attacks!
URL url = new URL("https://www.example.com/validateTokenID.php");
Map<String, Object> HTTPpostParams = new LinkedHashMap<>();
if (BuildConfig.DEBUG) {
//debug build
HTTPpostParams.put("build", "DEBUG");
}else{
//release build
HTTPpostParams.put("build", "RELEASE");
}
HTTPpostParams.put("mToken", MygoogleSignedInAccount.getIdToken());
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : HTTPpostParams.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder strB = new StringBuilder();
for ( int c = in.read(); c != -1; c = in.read() ) strB.append((char) c);
return strB.toString();
} catch (Exception e) {
//this.exception = e;
Log.e("MyApp", e.getMessage());
return e.getMessage();
}
}