バックエンドサーバーと通信するアプリでnow.ggログインを使用する場合、サーバー上で現在サインインしているユーザーを特定する必要がある場合があります。
バックエンドサーバーを使用してサインインしたユーザーを安全に識別するには、以下の手順に従ってください。
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トークンを受け取った後、Verify Token APIを使用してトークンの完全性を検証する必要があります。
import requests
try:
url = "https://now.gg/accounts/oauth2/v1/verify-token"
payload={
"token_type": "id_token",
"token": ,
"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);
});
トークンを確認したら、そのユーザーがすでにユーザーデータベースに登録されているかどうかを確認します。
※アプリで新しく作成されたユーザーを検出し、このユーザーをデータベースに保存すると、そのユーザーから必要な追加プロファイル情報を取得できます。
ユーザーアカウントサービス
ユーザーアカウントサービス
ドキュメント改訂版 1.0