【问题标题】:Alternative to using the Google API PHP Client使用 Google API PHP 客户端的替代方法
【发布时间】:2019-01-14 19:56:32
【问题描述】:

我已经结合使用 Google JS API 和 Google PHP API 为我的网站设置了 Google Connect。

整个 PHP API 包含 16MB 以上的 PHP 文件,极大地降低了我的本地开发环境的速度。我想删除所有这些臃肿并只使用 JS API,将检索到的值发送到带有 AJAX 的 PHP。这可能吗?

我当前的工作代码如下所示,但我想重构代码以完全不使用 PHP API。

jQuery

​​>
gapi.load('auth2', function() {

    var scopes = [
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile'
    ];

    // Retrieve the singleton for the GoogleAuth library and set up the client.
    gapi.auth2.authorize({
        'client_id': 'XXXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
        'cookie_policy': 'single_host_origin',
        'fetch_basic_profile': false,
        'ux_mode': 'popup',
        'scope': scopes.join(' '),
        'prompt': 'select_account'
    },
    function(googleResponse) {

        if ( googleResponse.error ) {
            return;
        }

        var data = {
            'action': 'social_connect_google',
            'access_token': googleResponse.access_token
        }

        $.ajax({
            url: ajax_url,
            type: 'POST',
            data: data,
            success: function(response) {
                if ( response.success === true ) {
                    console.log(response);                  
                }
            }
        });         
    });             
});

PHP

/**
 * Connect to Google API and login, signup or link accounts.
 *
 * @since  1.0.0
 *
 * @return WP_User|NS_Error
 */
public function connect() {
    $client = new Google_Client();

    $credentials = json_decode('XXXXXXXXX', true);

    $client->setAuthConfig($credentials);

    // Set Access Token
    $client->setAccessToken($_POST['access_token']);

    $oauth2 = new Google_Service_Oauth2($client);

    if ( !$oauth2 ) {
        return new NS_Error('invalid_access_token', 'The access_token was invalid.');   
    }

    $google_user = $this->get_user($oauth2->userinfo->get());

    if ( $this->get_user_by_google_id($google_user) ) {
        return $this->login($google_user);

    } else if ( $this->get_user_by_google_email($google_user) ) {
        return $this->link_accounts($google_user);

    } else {
        return $this->signup($google_user);
    }
}

基本上,我需要将 PHP 代码的 OAuth2 部分传输到 JavaScript 中,然后将检索到的 Google 用户数据发送到 PHP,从而跳过对 PHP API 的需要。

这可以做到吗?这是一种有效且安全的处理方式吗?

【问题讨论】:

    标签: google-api google-oauth google-api-php-client google-api-js-client


    【解决方案1】:

    是的,它有效且安全。

    通常,由您的用例决定 OAuth 最好在客户端还是在服务器上完成。

    如果您的用例适合基于服务器的 OAuth,并且库大小是一个问题,那么不要使用该库。通过直接调用 2 个端点来进行 OAuth 非常容易。另一方面,如果您的用例在客户端上运行良好,那么请继续执行客户端 OAuth。如果您使用 https 传输用户数据,则不会存在安全问题。

    请注意,客户端 OAuth 的最大限制是您无法请求刷新令牌。这意味着授权仅在用户存在时才有效,因此如果用户离线,您的服务器将无法代表用户执行工作。

    【讨论】:

    • 您能否更新您的答案以包含这两个提到的端点,或者包含一个指向正确方向的链接?我用 Google 搜索过,但找不到我要找的东西。
    • 在这里查看假人指南stackoverflow.com/questions/54097179/…
    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多