c – YouTube API v3 – 上传视频

我想用我的Qt5程序在YouTube上加载myVideo.avi.我通过OAuth 2.0成功授权并获取access_token而没有错误.

但是当我尝试使用API​​ https://developers.google.com/youtube/v3/docs/videos/insert
我得到一些错误!

QString googleApiUrl = "https://www.googleapis.com/upload/youtube/v3/videos?access_token="+authorisation->getAccessToken()+"&part=snippet";

    QNetworkRequest request;
    request.setUrl(QUrl(googleApiUrl));

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::MixedType);

    QHttpPart videoPart;
    videoPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("video/*"));
    videoPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("Slug"));

    m_video = new QFile(m_filePath);
    if (m_video->open(QIODevice::ReadOnly)) {
        videoPart.setBodyDevice(m_video);
    }
    multiPart->append(videoPart);


    m_networkManager = new QNetworkAccessManager();
    m_networkManager->post(request,multiPart);
    connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleNetworkReply(QNetworkReply*)));

响应-

"Host requires authentication"
"{
"error": {
 "errors": [
  {
   "domain": "youtube.header",
   "reason": "youtubeSignupRequired",
   "message": "Unauthorized",
   "locationType": "header",
   "location": "Authorization"
  }
 ],
 "code": 401,
 "message": "Unauthorized"
}
}
"

我做错了什么?

最佳答案 youtubeSignupRequired表示您正在浏览OAuth 2流程并使用尚未与之关联的YouTube频道的Google帐户授权访问权限.我建议再次浏览流程并确认您使用正确的帐户授权访问权限.

顺便说一句,我建议试试https://github.com/google/google-api-cpp-client.这是相当新的,可能会简化您的代码,而不是自己进行原始HTTP调用.

点赞