【问题标题】:Google Drive API - Invalid grant: Bad RequestGoogle Drive API - 无效授权:错误请求
【发布时间】:2016-10-12 17:20:04
【问题描述】:

我试图使用 PHP 实现 Google Drive API。这是我的目标,

  1. 向用户请求 Google Drive 文件夹 ID
  2. 输入后,验证用户并获取授权码
  3. 获取并设置访问令牌以从文件夹中获取文件详细信息

这里是代码示例,

PHP

public $google_client;

function __construct(){
    $this->google_client = new Google_Client();
    $this->google_client->setApplicationName(APPLICATION_NAME);
    $this->google_client->setScopes(SCOPES);
    $this->google_client->setAuthConfig(CLIENT_SECRET_PATH);
    $this->google_client->setAccessType('offline');
}

function get_drive_auth_url(){
    $folder_id = trim($_POST['folder_id']) || null;
    $auth_code = trim($_POST['auth_code']) || null;

    if(!$folder_id || !$auth_code){
        $auth_url = $this->google_client->createAuthUrl();
        $response = array('type' => true, 'message' => 'success', 'data' => $auth_url);
        echo json_encode($response);
        exit();
    } else {
        $access_token = $this->google_client->fetchAccessTokenWithAuthCode($auth_code);
        print_r($access_token);
        exit();
    }

}

JS

var folder_id = jQuery(this).val() || '';
if(!folder_id){
    alert('Please provide a valid folder id.');
    return false;
}

// AJAX Request to fetch the auth url
jQuery.ajax({
    url: woocommerce_params.ajax_url,
    method: 'POST',
    dataType: 'json',
    data: {
        action: 'get_drive_auth_url'
    },
    success:function(response) {
        var url = response.data || '';
        if(!url){
            return false;
        }

        window.open(url, 'Google Drive Authorization', 'width=600,height=350', true);
        var auth_code = prompt('Please provide the authorization code.');
        if(auth_code){
            //AJAX Request to pass the folder id and auth code
            jQuery.ajax({
                url: woocommerce_params.ajax_url,
                method: 'POST',
                dataType: 'json',
                data: {
                    action: 'get_drive_auth_url',
                    auth_code: auth_code,
                    folder_id: folder_id
                },
                success:function(res) {
                    console.log(res);
                },
                error: function(errorThrown){ console.log(errorThrown);
                    alert('Error: ' + errorThrown.status + ' ' + errorThrown.statusText);
                }
            });
        }
    },
    error: function(errorThrown){
        alert('Error: ' + errorThrown.status + ' ' + errorThrown.statusText);
    }
});

错误

Array( [error] => invalid_grant [error_description] => Bad Request)

感谢您的帮助!

【问题讨论】:

    标签: php google-api google-drive-api google-api-php-client drive


    【解决方案1】:

    这是我的错,

    替换下面的代码,

    $folder_id = trim($_POST['folder_id']) || null;
    $auth_code = trim($_POST['auth_code']) || null; // This returns 1
    

    $folder_id = trim($_POST['folder_id']);
    $auth_code = trim($_POST['auth_code']); // This return the actual auth code
    

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2013-07-18
      相关资源
      最近更新 更多