如何使用后端服务器对登录的用户进行安全鉴别:
userData.getUserId()
方法取得的用户ID。用HTTPS POST请求向 tokensignin
API发送ID令牌到您的后端服务器:
HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("https://yourbackend.example.com/tokensignin"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("idToken", idToken)); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); final String responseBody = EntityUtils.toString(response.getEntity()); Log.i(TAG, "Signed in as: " + responseBody); } catch (ClientProtocolException e) { Log.e(TAG, "Error sending ID token to backend.", e); } catch (IOException e) { Log.e(TAG, "Error sending ID token to backend.", e); }
通过HTTPS POST,在您的后端服务器上接收到ID令牌之后,您必须使用 验证令牌API 来验证令牌的完整性。
import requests try: url = "https://now.gg/accounts/oauth2/v1/verify-token" payload={ "token_type": "id_token", "token": < id_token > , "client_id": < your_oauth_client_id > } headers = { 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=json.dumps(payload)) print(response.text) #token verified except: #token not verified pass
var axios = require('axios'); var config = { method: 'post', url: 'https://now.gg/accounts/oauth2/v1/verify-token', headers: { 'Content-Type': 'application/json' }, data: { "token_type": "id_token", "token": < id_token > , "client_id": < your_oauth_client_id > } }; axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
在您完成令牌验证之后,检查该用户是否已经存在于您的用户数据库中。
注意:当在您的应用中检测到一个新创建的用户,且在您的数据库中保存此用户时,您可以获取任何您需要的该用户的其它个人资料。