代码之家  ›  专栏  ›  技术社区  ›  Mahidul Islam

刷新的googleapi无法从驱动器获取文件列表。谷歌服务异常(错误400)

  •  0
  • Mahidul Islam  · 技术社区  · 8 年前

    我正在尝试使用googledriveapiv2和Google的PHP客户端库将我的web应用程序与googledrive连接起来( "google/apiclient": "^2.0" ).

    Google_Service_Exception (Err 400) :

    您的客户发出了格式错误或非法的请求。

    enter image description here

    我已成功获取访问和刷新令牌:

    {
     "access_token":"**********",
     "expires_in":3600,
     "refresh_token":"1/********",
     "scope":"https://www.googleapis.com/auth/drive",
     "token_type":"Bearer",
     "created":1539085127
    }
    

    我尝试通过以下方式刷新令牌:

    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    }
    

    这样:

    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
    }
    

    我怎样才能妥善保管一个多小时收到的证件?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Linda Lawton - DaImTo    8 年前

    听起来你的客户端设置有问题。试试这个

    function getOauth2Client() {
        try {
    
            $client = buildClient();
    
            // Set the refresh token on the client. 
            if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
                $client->refreshToken($_SESSION['refresh_token']);
            }
    
            // If the user has already authorized this app then get an access token
            // else redirect to ask the user to authorize access to Google Analytics.
            if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    
                // Set the access token on the client.
                $client->setAccessToken($_SESSION['access_token']);                 
    
                // Refresh the access token if it's expired.
                if ($client->isAccessTokenExpired()) {              
                    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
                    $client->setAccessToken($client->getAccessToken()); 
                    $_SESSION['access_token'] = $client->getAccessToken();              
                }           
                return $client; 
            } else {
                // We do not have access request access.
                header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
            }
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }
    }
    

    Oauth2Authentication.php oauth2callback.php

        2
  •  0
  •   Mahidul Islam    8 年前

    您可以尝试使用curl获取文件列表。

    如果你想从谷歌硬盘获取所有文件。

    在curl中使用此Url:

    https://www.googleapis.com/drive/v2/files
    

    在curl中使用此Url:

    https://www.googleapis.com/drive/v2/files?q='".$yourDriveFolderId."'+in+parents
    

    Google Drive Documentation

    推荐文章