dongzhenshen7435 2015-08-29 06:03
浏览 55

我应该如何使用Google oAuth 2.0?

What I'm trying to achieve is to allow anyone to login to the site and only certain registered user with permission are allowed launch the "Tap this button..."

The thing is, I'm very uncertain as to how to verify whether the user is logged in and has legitimate token to access the button. Should I store the token on client's browser and push it to the server when the button is pressed, is that safe?

Should I be using PHP in this case?

My main page:

enter image description here

Javascript & AJAX:

var token;
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
token = googleUser.getAuthResponse().id_token; // should I store the token?

var form = {
"ID": profile.getId(),
    "Name": profile.getName(),
    "Email": profile.getEmail(),
    "Image_URL": profile.getImageUrl(),
}
$.ajax({
    method: "POST",
    url: "php/verification.php", // register account and save to db.
    data: form,
    dataType: "json",
    success: function(data) {
        console.log(data.result)
    }
})
}

I'm also very vague with the documentation: https://developers.google.com/identity/sign-in/web/backend-auth

Will be very happy if there's anybody could enlighten my path and clear up my frustration.

  • 写回答

1条回答 默认 最新

  • dongshi7350 2015-09-03 20:36
    关注

    Yes, that is the recommended way to do it in the documentation you linked.

    Since you're using PHP, you'll verify the token using Google's PHP SDK. Here's a simple example:

    /* Create the client, and add your credentials */
    $client = new Google_Client();
    $client->setClientId('your_client_id');
    $client->setClientSecret('your_client_secret');
    
    /* Verify the token you received */
    $ticket = $client->verifyIdToken($_POST['token']);
    
    /* Get the response details */
    $data = $ticket->getAttributes();
    
    /* Retrieve user's unique Google ID */
    echo json_encode(['user_id' => $data['payload']['sub']]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题