首页 > 解决方案 > 错误 400:invalid_request 提示参数值无效:无效提示:缺点

问题描述

我正在 WordPress 中创建一个插件,我试图让 Google 工作表脚本工作,但我找不到方法。

我尝试了此处可用的所有代码和不同的来源,但我找不到任何使它起作用的东西。这是我得到的错误:

在浏览器中打开以下链接: https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=390081316396-0es6irrbq1ccj01uvhlqvrbc0eo9eopq.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fwww.suma。 pro%2Fapp%2F&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets.readonly&prompt=consent 输入验证码:致命错误:Uncaught InvalidArgumentException: Invalid code in /home/suma/public_html/app/wp-content/plugins/suma-app/inc/vendor/google/apiclient/src/Google/Client.php:176 Stack跟踪:#0 /home/suma/public_html/app/wp-content/plugins/suma-app/suma-app.php(125): Google_Client->fetchAccessTokenWithAuthCode('') #1 /home/suma/public_html/app /wp-includes/class-wp-hook.php(292): SumaApp->getClient('') #2 /home/suma/public_html/app/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Array) #3 /home/suma/public_html/app/wp-includes/plugin.php(484): WP_Hook->do_action(Array) #4 /home/suma/public_html/app/wp -settings.php(560): do_action('init') #5 /home/suma/public_html/app/wp-config.php(90): require_once('/home/suma/publ...') #6 /home/suma/public_html/app/wp-load.php(37):require_once('/home/suma/publ...') #7 /home/suma/public_html/app/wp-blog-header.php(13): require_once('/home/suma/publ...') #8 /home/suma/public_html/app/index.php(17): require('/home/suma/publ...') #9 {main} 抛出 /home/suma/public_html/app/wp-第 176 行的 content/plugins/suma-app/inc/vendor/google/apiclient/src/Google/Client.php

    function getClient()
    {
    define('STDIN',fopen("php://stdin","r"));
    $client = new Google_Client();
    $client->setApplicationName('Sumaapptest');
    $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
    $client->setAuthConfig(plugin_dir_path(__FILE__) .'inc/vendor/credentials.json');
    $client->setAccessType('offline');
    $client->setRedirectUri( 'https://www.suma.pro/app/');
    $client->setPrompt('consent');

    // Load previously authorized token from a file, if it exists.
    // The file token.json stores the user's access and refresh tokens, and is
    // created automatically when the authorization flow completes for the first
    // time.
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    // If there is no previous token or it's expired.
    if ($client->isAccessTokenExpired()) {
        // Refresh the token if possible, else fetch a new one.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            // Exchange authorization code for an access token.
            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
            $client->setAccessToken($accessToken);

            // Check to see if there was an error.
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }
        // Save the token to a file.
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
    }

    function suma_order_status_change_custom($this_get_id){

    // Get the API client and construct the service object.
    $client = getClient();
    $service = new Google_Service_Sheets($client);

    // Prints the names and majors of students in a sample spreadsheet:
    // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
    $spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms';
    $range = 'Class Data!A2:E';
    $response = $service->spreadsheets_values->get($spreadsheetId, $range);
    $values = $response->getValues();

    if (empty($values)) {
    print "No data found.\n";
    } else {
    print "Name, Major:\n";
    foreach ($values as $row) {
        // Print columns A and E, which correspond to indices 0 and 4.
        printf("%s, %s\n", $row[0], $row[4]);
    }
    }


    // echo $this_get_id;
    //   die();
    }

标签: wordpressgoogle-sheets-api

解决方案


检查您的 OAuth 客户端 ID

转到https://console.developers.google.com/

导航到 APIs & Services 并查看此屏幕。

您应该看到客户端 ID 的类型是 Web 应用程序。您收到的消息看起来像是您已将其设为“桌面”类型。

确保您已完全按照快速入门中的步骤进行操作,然后在快速入门正常运行后使其与 wordpress 一起使用。

在此处输入图像描述


推荐阅读