将文件上传到随机文件夹

时间:2016-12-13 09:39:16

标签: c++ google-drive-api qt4 blackberry-10 google-drive-realtime-api

我希望用户将文件上传到Google云端硬盘,但不能上传到根版。 Children资源只允许从根目录列出文件夹(这是有意义的),但不会将文件夹的名称放在JSON响应中。

我无法使用父资源,因为fileID不存在,也因为我需要用户查看文件夹(在各自的层次结构中)并选择他们选择的文件夹。这是我到目前为止所尝试的

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);
}

其中folderID是&#34; root&#34;。我得到的回应是:

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。

2 个答案:

答案 0 :(得分:0)

一些指示......

  1. 您正在使用v2 API。如果这是一个新的应用程序,您应该切换到v3。
  2. 您的REST呼叫正在请求子资源列表。如果您查看https://developers.google.com/drive/v2/reference/children,您会发现儿童资源中没有标题/名称。
  3. 实现您正在寻找的内容的更常见方法是使用查询参数folder_id in parents执行files.list。

答案 1 :(得分:0)

是的,这是非常错误的......直到现在。对于记录,任何上传,无论是文件还是文件夹,到任何位置,无论是root还是任何RANDOM文件夹,您都需要该特定文件夹的folder_id。使用“root”作为根文件夹的id。要上传,您不需要selfLink或parentLink。只是

parents:[{ "id" : "id of the folder, use root for root folder" }]

谢谢@pinoyyid。