API Google Диска — недействительный грант: неверный запрос

Я пытался реализовать API Google Диска с помощью PHP. Вот моя цель,

  1. Запросить идентификатор папки Google Диска у пользователя
  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)

Ценю твою помощь!


person moustacheman    schedule 12.10.2016    source источник


Ответы (1)


Это была моя ошибка,

Замените ниже код,

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

to

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