0

when I try to want access the token the verification code is not working and they give me error undefined constant "STDIN" im new here in google API so please help me what's I do? Thanks

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()));
}
  • `STDIN` is only available when running scripts from the CLI, not a webserver. How are you running this script? – Barmar Feb 18 '21 at 06:36
  • how can I fix this error its my project can you help me in this ? –  Feb 18 '21 at 06:38
  • If you're running the script from a web page, you get parameters using `$_POST` or `$_GET` depending on how the form was submitted. – Barmar Feb 18 '21 at 06:39
  • Any tutorial on processing forms in PHP will explain this. – Barmar Feb 18 '21 at 06:40
  • sir when i use $_POST or $_GET then they give me this type of error "Fatal error: Uncaught Error: Array callback must have exactly two elements in C:\xampp\htdocs\imagebob\google-drive.php:41 Stack trace: #0 C:\xampp\htdocs\imagebob\google-drive.php(62): getClient() #1 {main} thrown in C:\xampp\htdocs\imagebob\google-drive.php on line 41" –  Feb 18 '21 at 06:47
  • Without seeing the code I have no way of knowing what that refers to. – Barmar Feb 18 '21 at 06:48
  • @barmar in this link the complete code is available with complete classes can you fix my this error https://drive.google.com/file/d/1BDjyxK5uaGoEogLwq5TJ20gAu6oPAr29/view?usp=sharing –  Feb 18 '21 at 06:53
  • I'm not going to download the full code. Post a [mcve] here. – Barmar Feb 18 '21 at 06:56
  • sir i just replace the $authCode = trim(fgets(STDIN)); into $authCode = $_POST(fgets(STDIN)); then they show me error this type --> Fatal error: Uncaught Error: Array callback must have exactly two elements in C:\xampp\htdocs\imagebob\google-drive.php:41 Stack trace: #0 C:\xampp\htdocs\imagebob\google-drive.php(62): getClient() #1 {main} thrown in C:\xampp\htdocs\imagebob\google-drive.php on line 41 –  Feb 18 '21 at 07:02
  • `$_POST` is not a function, it's an array. And you can't use `fgets(STDIN)` since there's no `STDIN`.\ – Barmar Feb 18 '21 at 07:03
  • It should be `$authCode = $_POST['inputname']` where `inputname` corresponds to the `name` attribute of the input field in the form. – Barmar Feb 18 '21 at 07:04
  • the error is same like above –  Feb 18 '21 at 07:11
  • sir can you give me any tutorial on processing forms in PHP will explain this –  Feb 18 '21 at 07:14
  • Sorry, I don't know tutorials very well. But wherever you learned PHP, I would expect it to have a section on this, as it's one of the most common uses. – Barmar Feb 18 '21 at 07:20
  • ok thanks a lot sir –  Feb 18 '21 at 07:46
  • Can you post the whole code you are using? @ShujaatKhattak – ale13 Feb 19 '21 at 13:28
  • @ale13 in this link there is my complete code --> https://drive.google.com/file/d/1BDjyxK5uaGoEogLwq5TJ20gAu6oPAr29 –  Feb 20 '21 at 05:40
  • Please edit the question and include the code here @ShujaatKhattak – ale13 Feb 22 '21 at 15:14
  • @ale13 i upload again my question and include my code the link is there https://stackoverflow.com/questions/66367003/upload-image-in-google-drive-using-google-drive-api-using-the-php-script-and-run –  Feb 25 '21 at 10:59

1 Answers1

0

The example code on the quickstart tells you use the php command. This means that you are executing it on a console/terminal where you have STDIN.

If you are trying to do a web server application, you don't have it and thus the code is different. You can find the most common setups on the library's GitHub repository (see examples). Take a look to them, understand their differences, and choose the one that's best for you.

Martí
  • 2,651
  • 1
  • 4
  • 11