将文件上传到随机文件夹
Upload file into a random folder
我希望用户将文件上传到 Google 驱动器,而不是根目录。 Children 资源仅允许从根目录开始列出文件夹(这是有道理的),但不会将文件夹的名称放入 JSON 响应中。
我无法使用父资源,因为文件 ID 不存在,还因为我需要用户查看文件夹(在各自的层次结构中)并选择他们选择的文件夹。这是我到目前为止尝试过的
void DriveClass::listFileFolderDrive(const QString& folderID){
QUrl url("https://www.googleapis.com/drive/v2/files/" + folderID + "/children");
//qDebug()<<" url for About Drive: "<< url;
QNetworkRequest* request = new QNetworkRequest( url );
request->setHeader( QNetworkRequest::ContentTypeHeader, "application/json" );
request->setRawHeader( "Authorization", QString( "Bearer %1" ).arg( this->_oAuth2->token() ).toLatin1() );
QNetworkReply* reply = m_networkManager->get(*request);
bool result = connect(reply, SIGNAL(finished()), this, SLOT(onlistFileFolderDriveFinished()));
Q_ASSERT(result);
Q_UNUSED(result);
}
其中文件夹 ID 为 "root"。我得到的回复是:
QVariant(QVariantMap, QMap(("etag", QVariant(QString, ""dyf6dc5nFpQUIZ2nJvOu2LyLJk8/bD3mjJonFeCjTc8Eu8VRx_DzPtg"") ) ( "items" , QVariant(QVariantList, (QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_Ycab1hhT3I0T3ptMDg") ) ( "id" , QVariant(QString, "0B_0rcYNi_Ycab1hhT3I0T3ptMDg") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_Ycab1hhT3I0T3ptMDg") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcabXdJd21CeWRHUXM") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcabXdJd21CeWRHUXM") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcabXdJd21CeWRHUXM") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaM1ZCOXBaZzJSNmc") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaM1ZCOXBaZzJSNmc") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaM1ZCOXBaZzJSNmc") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaSmtNMmRlTld5QkU") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaSmtNMmRlTld5QkU") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaSmtNMmRlTld5QkU") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaY0I0czA4djNLT1U") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaY0I0czA4djNLT1U") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaY0I0czA4djNLT1U") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaTW8zZklsNnFNTDBmQ1dodi1yTFhqX2s4SzNr") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaTW8zZklsNnFNTDBmQ1dodi1yTFhqX2s4SzNr") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaTW8zZklsNnFNTDBmQ1dodi1yTFhqX2s4SzNr") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/1_eVyijBnZPvFfn4WewtScnlSXRFpXInmop-AUD6Jp5nSV4rnxL4-IpLZ6uoR") ) ( "id" , QVariant(QString, "1_eVyijBnZPvFfn4WewtScnlSXRFpXInmop-AUD6Jp5nSV4rnxL4-IpLZ6uoR") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/1_eVyijBnZPvFfn4WewtScnlSXRFpXInmop-AUD6Jp5nSV4rnxL4-IpLZ6uoR") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaYmNmZTEwMzYtNjgzZC00MjJkLWFmYWQtZmRlODc4OTEwZTVh") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaYmNmZTEwMzYtNjgzZC00MjJkLWFmYWQtZmRlODc4OTEwZTVh") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaYmNmZTEwMzYtNjgzZC00MjJkLWFmYWQtZmRlODc4OTEwZTVh") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/1CHu3CzJ23I6WS3J8rOIpT5szN8r8mTOxztiWixwXafQ") ) ( "id" , QVariant(QString, "1CHu3CzJ23I6WS3J8rOIpT5szN8r8mTOxztiWixwXafQ") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/1CHu3CzJ23I6WS3J8rOIpT5szN8r8mTOxztiWixwXafQ") ) ) ) ) ) ) ( "kind" , QVariant(QString, "drive#childList") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/root/children") ) ) )
我希望你说得够清楚了。我在 QVariant 中解析它以便在 ListView 中使用结果。根据我对 google 驱动器文档的研究,需要一个文件在插入之前有一个 folderID。而且我没有要上传的新文件的 folderID。
几点建议...
- 您正在使用 v2 API。如果这是一个新应用程序,您可能应该切换到 v3。
- 您的 REST 调用正在请求子资源列表。如果您查看 https://developers.google.com/drive/v2/reference/children,您会发现子资源中没有 title/name。
- 实现所需内容的更常见方法是使用
folder_id in parents
. 查询参数执行 files.list
是的,错了……直到现在。作为记录,任何上传,无论是文件还是文件夹,到任何位置,无论是根目录还是任何 RANDOM 文件夹,您都需要该特定文件夹的 folder_id。使用 'root' 作为根文件夹的 ID。要上传,您不需要 selfLink 或 parentLink。刚刚
parents:[{ "id" : "id of the folder, use root for root folder" }]
谢谢@pinoyyid。
我希望用户将文件上传到 Google 驱动器,而不是根目录。 Children 资源仅允许从根目录开始列出文件夹(这是有道理的),但不会将文件夹的名称放入 JSON 响应中。
我无法使用父资源,因为文件 ID 不存在,还因为我需要用户查看文件夹(在各自的层次结构中)并选择他们选择的文件夹。这是我到目前为止尝试过的
void DriveClass::listFileFolderDrive(const QString& folderID){
QUrl url("https://www.googleapis.com/drive/v2/files/" + folderID + "/children");
//qDebug()<<" url for About Drive: "<< url;
QNetworkRequest* request = new QNetworkRequest( url );
request->setHeader( QNetworkRequest::ContentTypeHeader, "application/json" );
request->setRawHeader( "Authorization", QString( "Bearer %1" ).arg( this->_oAuth2->token() ).toLatin1() );
QNetworkReply* reply = m_networkManager->get(*request);
bool result = connect(reply, SIGNAL(finished()), this, SLOT(onlistFileFolderDriveFinished()));
Q_ASSERT(result);
Q_UNUSED(result);
}
其中文件夹 ID 为 "root"。我得到的回复是:
QVariant(QVariantMap, QMap(("etag", QVariant(QString, ""dyf6dc5nFpQUIZ2nJvOu2LyLJk8/bD3mjJonFeCjTc8Eu8VRx_DzPtg"") ) ( "items" , QVariant(QVariantList, (QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_Ycab1hhT3I0T3ptMDg") ) ( "id" , QVariant(QString, "0B_0rcYNi_Ycab1hhT3I0T3ptMDg") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_Ycab1hhT3I0T3ptMDg") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcabXdJd21CeWRHUXM") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcabXdJd21CeWRHUXM") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcabXdJd21CeWRHUXM") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaM1ZCOXBaZzJSNmc") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaM1ZCOXBaZzJSNmc") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaM1ZCOXBaZzJSNmc") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaSmtNMmRlTld5QkU") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaSmtNMmRlTld5QkU") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaSmtNMmRlTld5QkU") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaY0I0czA4djNLT1U") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaY0I0czA4djNLT1U") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaY0I0czA4djNLT1U") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaTW8zZklsNnFNTDBmQ1dodi1yTFhqX2s4SzNr") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaTW8zZklsNnFNTDBmQ1dodi1yTFhqX2s4SzNr") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaTW8zZklsNnFNTDBmQ1dodi1yTFhqX2s4SzNr") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/1_eVyijBnZPvFfn4WewtScnlSXRFpXInmop-AUD6Jp5nSV4rnxL4-IpLZ6uoR") ) ( "id" , QVariant(QString, "1_eVyijBnZPvFfn4WewtScnlSXRFpXInmop-AUD6Jp5nSV4rnxL4-IpLZ6uoR") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/1_eVyijBnZPvFfn4WewtScnlSXRFpXInmop-AUD6Jp5nSV4rnxL4-IpLZ6uoR") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/0B_0rcYNi_YcaYmNmZTEwMzYtNjgzZC00MjJkLWFmYWQtZmRlODc4OTEwZTVh") ) ( "id" , QVariant(QString, "0B_0rcYNi_YcaYmNmZTEwMzYtNjgzZC00MjJkLWFmYWQtZmRlODc4OTEwZTVh") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/0B_0rcYNi_YcaYmNmZTEwMzYtNjgzZC00MjJkLWFmYWQtZmRlODc4OTEwZTVh") ) ) ) , QVariant(QVariantMap, QMap(("childLink", QVariant(QString, "https://www.googleapis.com/drive/v2/files/1CHu3CzJ23I6WS3J8rOIpT5szN8r8mTOxztiWixwXafQ") ) ( "id" , QVariant(QString, "1CHu3CzJ23I6WS3J8rOIpT5szN8r8mTOxztiWixwXafQ") ) ( "kind" , QVariant(QString, "drive#childReference") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/0AP0rcYNi_YcaUk9PVA/children/1CHu3CzJ23I6WS3J8rOIpT5szN8r8mTOxztiWixwXafQ") ) ) ) ) ) ) ( "kind" , QVariant(QString, "drive#childList") ) ( "selfLink" , QVariant(QString, "https://www.googleapis.com/drive/v2/files/root/children") ) ) )
我希望你说得够清楚了。我在 QVariant 中解析它以便在 ListView 中使用结果。根据我对 google 驱动器文档的研究,需要一个文件在插入之前有一个 folderID。而且我没有要上传的新文件的 folderID。
几点建议...
- 您正在使用 v2 API。如果这是一个新应用程序,您可能应该切换到 v3。
- 您的 REST 调用正在请求子资源列表。如果您查看 https://developers.google.com/drive/v2/reference/children,您会发现子资源中没有 title/name。
- 实现所需内容的更常见方法是使用
folder_id in parents
. 查询参数执行 files.list
是的,错了……直到现在。作为记录,任何上传,无论是文件还是文件夹,到任何位置,无论是根目录还是任何 RANDOM 文件夹,您都需要该特定文件夹的 folder_id。使用 'root' 作为根文件夹的 ID。要上传,您不需要 selfLink 或 parentLink。刚刚
parents:[{ "id" : "id of the folder, use root for root folder" }]
谢谢@pinoyyid。