访问Google Api以获得具有有效访问和刷新令牌的文件列表



我正在为应用程序使用Oauth类来访问Google drive API,我有刷新和访问令牌,现在我所需要的就是设置请求的参数。

我的问题是,我似乎找不到获得适当响应所需的参数,我查看了OAuth游乐场,发送的请求有三个标头AuthorizationHostContent length

我正在使用的类应该正确地处理这些头,我确信它确实正确地接收了codeaccess/refresh tokens

当我发送请求时,谷歌会返回一个错误;

StdClass Object
(
[error] => stdClass Object
    (
        [errors] => Array
            (
                [0] => stdClass Object
                    (
                        [domain] => global
                        [reason] => authError
                        [message] => Invalid Credentials
                        [locationType] => header
                        [location] => Authorization
                    )
            )
        [code] => 401
        [message] => Invalid Credentials
    )
)


这肯定表示凭据无效?但如果我刚刚收到"新鲜"访问令牌和刷新令牌,这肯定可以吗?这是我正在发送的请求(根据OAuth类方法)。

$row = $this->docs_auth->row();
$this->client                = new oauth_client_class;
$this->client->server        = 'Google';
$this->client->redirect_uri  = 'https://localhost/p4a/applications/reflex_application/index.php';
$this->client->debug         = true;
$this->client->client_id     = REFLEX_GOOGLE_CLIENT;
$this->client->client_secret = REFLEX_GOOGLE_SECRET;
$this->client->access_token  = $row['access_token'];
$this->client->refresh_token = $row['refresh_token'];

$url = 'https://www.googleapis.com/drive/v2/files';
$Values = array(
    'access_token'  => $this->client->access_token,
    'client_id'     => $this->client->client_id,
    'client_secret' => $this->client->client_secret
);
/*
 * Request: GET https://www.googleapis.com/drive/v2/files
 * $values = the values sent in the request
 * $folder = the response returned from Google.
 */
$this->client->callAPI($url, 'GET', $values, array(
    'FailOnAccessError' => false
), $folder);

$this->field->setValue(print_r($folder, true));

所以我的问题是,发送到谷歌以获得文件夹和文件列表的正确参数是什么,请求所需的标题是什么(我不想编辑太多类,但已经需要编辑了)。

感谢您抽出时间

查看您发布的链接和原始类创建者编写的示例,您可以在调用callAPI()之前对类调用Initialize()。

以下是他使用的示例:

if(($success = $client->Initialize()))
{
    if(($success = $client->Process()))
    {
        if(strlen($client->authorization_error))
        {
            $client->error = $client->authorization_error;
            $success = false;
        }
        elseif(strlen($client->access_token))
        {
            $success = $client->CallAPI(
                'https://www.googleapis.com/oauth2/v1/userinfo',
                'GET', array(), array('FailOnAccessError'=>true), $user);
        }
    }
    $success = $client->Finalize($success);
}

离开这个几个月后,我终于找到了你想要的方法,尽管我使用了谷歌自己的类:

方法大致相同;

首先用$this->client = new Google_Client(); 调用类

然后设置获取特定客户端响应所需的所有元数据,设置范围并设置访问类型:

    // Get your credentials from the APIs Console
    $this->client->setClientId($this->client_id);
    $this->client->setClientSecret($this->client_secret);
    $this->client->setRedirectUri($this->redirect_uri);
    $this->client->setScopes(array('https://www.googleapis.com/auth/drive ','https://www.googleapis.com/auth/drive.file' ));
        $this->client->setAccessType("offline");

然后最终获得存储的访问令牌(在数据库中或存储在会话中),并使用Google_DriveService($this->client)和类中的这些函数来执行文件列表:

try{
            $json = json_encode($this->loadAccessTokenFromDB());
            $this->client->setAccessToken($json);
            $this->client->setUseObjects(true);
            $service = new Google_DriveService($this->client);
            $parameters = array();
            $parameters['q'] = " FullText contains '" . $searchString . "'";
            $files = $service->files->listFiles($parameters);
            $ourfiles = $files->getItems();
            $fileArray = array();
            foreach ( $ourfiles as $file )
            {
                $fileArray[] = array(
                        'title'          => $file->getTitle(),
                        'id'             => $file->getID(),
                        'created'        => $file->getCreatedDate(),
                        'embedlink'      => $file->getEmbedLink(),
                        'exportlinks'    => $file->getExportLinks(),
                        'thumblink'      => $file->getThumbnailLink(),
                        'mimeType'       => $file->getMimeType(),
                        'webContentLink' => $file->getWebContentLink(),
                        'alternateLink'  => $file->getAlternateLink(),
                        'permissions'    => $file->getUserPermission()
                );
            }
            //$this->mimeType = $file->getMimeType();
            $this->documents->load($fileArray);
            if ($fileArray["id"] !== "")
            {
                $this->documents->firstRow();
                return;
            }
        } catch(Google_AuthException $e) {
            print $e->getMessage();
        }
        return;
    }

我也在测试可以使用的搜索字符串,从我设法得到的测试来看,该字符串必须是一个没有中断的字符串,它将搜索包含该特定字符串的任何内容,例如

foo会给出包含以下单词的文档:foo foobar等,但找不到foo bar,所以你必须小心,理想情况下,如果是用户的特定文档或其他内容,你应该寻找一个特定的唯一字符串来搜索,

再次感谢。

相关内容

  • 没有找到相关文章

最新更新