dongqian6554 2019-05-25 22:25
浏览 64

google登录重定向页面后如何获取表单输入值[用户点击表单上的按钮重定向到谷歌登录]

This is a simple system where for users to like a product they have to sign in to google in order to identify them. The button clicked contains value of product_id but I cant seem to figure out how to retrieve it after sign in and getting redirected to another page after sign in. Notice that the form action redirects user to google sign in.

 <form method="POST" action="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' . 
urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') .
'&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">

<button name="upvote"  type="submit" class="btn btn-info" value="<?php echo $product_id; ?>">Like</button>
</form>

The destination (Redirecting URL) after user has signed in is receiver.php

Here is my full working code;

1.settings.php THIS IS TO SIMPLY CONFIGURE THE API


    <?php

    /* Google App Client Id */
    define('CLIENT_ID', '/*My client ID*/');

    /* Google App Client Secret */
    define('CLIENT_SECRET', '/*My client Secret*/');

    /* Google App Redirect Url */
    define('CLIENT_REDIRECT_URL', 'https://www.example.com/receier.php');

    ?>

2. google-login-api.php TO SEND MY KEYS TO GOOGLE AND TO GET USER DATA AFTER SIGNING IN


    <?php

    class GoogleLoginApi
    {
        public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {  
            $url = 'https://www.googleapis.com/oauth2/v4/token';            

            $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
            $ch = curl_init();      
            curl_setopt($ch, CURLOPT_URL, $url);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
            curl_setopt($ch, CURLOPT_POST, 1);      
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);    
            $data = json_decode(curl_exec($ch), true);
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      
            if($http_code != 200) 
                throw new Exception('Error : Failed to receieve access token');

            return $data;
        }

        public function GetUserProfileInfo($access_token) { 
            $url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';          

            $ch = curl_init();      
            curl_setopt($ch, CURLOPT_URL, $url);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
            $data = json_decode(curl_exec($ch), true);
            $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);     
            if($http_code != 200) 
                throw new Exception('Error : Failed to get user information');

            return $data;
        }
    }

    ?>

3. likeproduct.php THIS IS WHERE THE BUTTON THAT REDIRECTS TO GOOGLE SIGN IN IS CLICKED, THE BUTTON CONTAINS THE VALUE OF THE PRODUCT ID



    <form method="POST" action="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' . 
    urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') .
    '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">

    <button name="upvote"  type="submit" class="btn btn-info" value="<?php echo $product_id; ?>">Like</button>
    </form>

4.receiver.php THIS IS WHERE USER GOOGLE ACCOUNT DETAILS ARE OBTAINED (REDIRECTING URL) AND ALSO WHERE I INTENT TO GET THE $product_id THAT WAS PASSED AS VALUE OF THE LIKE BUTTON IN likeproduct.php


    <?php
    require_once('settings.php');
    require_once('google-login-api.php');

    // Google passes a parameter 'code' in the Redirect Url
    if(isset($_GET['code'])) {
        try {
            $gapi = new GoogleLoginApi();

            // Get the access token 
            $data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);

            // Get user information
            $user_info = $gapi->GetUserProfileInfo($data['access_token']);
        }
        catch(Exception $e) {
            echo $e->getMessage();
            exit();
        }
    }
    ?>

How can I get the product id on receiver.php after submit?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答