我能够获取访问令牌和刷新令牌,但是当我尝试发出POST请求以将文件上传到Google云端硬盘时,出现401错误。我相信我没有正确设置请求的主体以上传文件(仍然不能100%知道如何做到),但是如果那是问题,我预计会出现400错误。
我尝试了https://airbrake.io/blog/http-errors/401-unauthorized-error此处提供的说明,例如清除浏览器缓存和登录/注销帐户。我还确保我使用的是Google在其文档中列出的正确URL。我的第一种方法负责获取访问令牌。第二个负责上传。
void
googleDriveInterface::getAuthentication() {
google = new QOAuth2AuthorizationCodeFlow;
google->setScope("https://www.googleapis.com/auth/drive");
QObject::connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl); //this connection opens authorization URL in user's default browser
QJsonDocument credentials = getCredentials("googleDriveCredentials.json"); //json file data is loaded into credentials
//parse JSON
const auto object = credentials.object();
const auto settingsObject = object["installed"].toObject();
const QUrl authUri(settingsObject["auth_uri"].toString());
const auto clientID = settingsObject["client_id"].toString();
const QUrl tokenUri(settingsObject["token_uri"].toString());
const auto clientSecret(settingsObject["client_secret"].toString());
const auto redirectUris = settingsObject["redirect_uris"].toArray();
const QUrl redirectUri(redirectUris[0].toString()); //get first URI
const auto port = static_cast<quint16>(redirectUri.port()); // port needed to for QOAuthHttpServerReplyHandler
google->setAuthorizationUrl(authUri);
google->setClientIdentifier(clientID);
google->setAccessTokenUrl(tokenUri);
google->setClientIdentifierSharedKey(clientSecret);
auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
google->setReplyHandler(replyHandler);
QObject::connect(google, SIGNAL(granted()), this, SLOT(slotSetAuthToken())); //save token when access is granted
QObject::connect(google, SIGNAL(granted()), this, SLOT(testSlot()));
google->grant(); //start authorization process
}
void
googleDriveInterface::uploadFile(const QString &filePath) {
QUrl uploadURL("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable");
QNetworkRequest request(uploadURL);
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "unable to open: " << filePath << " for upload:" << file.errorString();
return;
}
//get size of file and save as qbytearray for request header
QByteArray fileSize;
fileSize.setNum(file.size());
//get MIME type of file
QMimeDatabase mimeDB; //contains a database of all MIME types
QMimeType mime = mimeDB.mimeTypeForFile(filePath);
QByteArray mimeType = mime.name().toUtf8();
QByteArray test = authToken.toUtf8();
//set headers
request.setRawHeader("Authorization", authToken.toUtf8()); //convert authToken to QByteArray when we set header;
request.setRawHeader("Content-Type", "application/json; charset=UTF-8");
request.setRawHeader("Content-Length", fileSize);
request.setRawHeader("X-Upload-Content-Type", mimeType);
QByteArray fileData = file.readAll();
file.close();
networkReply = networkManager->post(request, fileData);
QObject::connect(networkReply, SIGNAL(metaDataChanged()), this, SLOT(testSlot1()));
}
我期望获得200个状态,但如前所述,我正在获得错误401错误。
答案 0 :(得分:2)
未经授权的错误401
基本上意味着您所提出的请求尚未得到授权。
我不是C ++开发人员,这只是我的oauth2知识的猜测。据我所见,访问令牌必须是不记名令牌。
request.setRawHeader("Authorization", "bearer " authToken.toUtf8());