从 iOS 应用程序 returns 上传视频到 youtube 400

Uploading a video to youtube from iOS app returns 400

我有一个 iOS(swift) 应用程序,我可以在其中通过我的 google 帐户登录并获取访问令牌。

检索访问令牌后,我向 YouTube 发出 post 上传视频的请求。

返回错误如下

{error =
{code = 400;
errors =({domain = global;
location = part;
locationType = parameter;
message = \"Required parameter: part\";
reason = required;});
message = \"Required parameter: part\";};}"

以下是我用来发出 post 请求的代码。

 func postVideoToYouTube(token: String, callback: @escaping (Bool) -> Void){
        if videoPath == nil {
            return
        }
        var movieData: NSData?
        do {
            movieData = try NSData(contentsOfFile: (videoPath?.relativePath)!, options: NSData.ReadingOptions.alwaysMapped)
        } catch _ {
            movieData = nil
            return
        }


        let headers = ["Authorization": "Bearer \(token)"]
        let URL = try! URLRequest(url: "https://www.googleapis.com/upload/youtube/v3/videos", method: .post, headers: headers)
        print("Video Data",movieData)
        Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(movieData as! Data, withName: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
        }, with: URL, encodingCompletion: {
            encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint("SUCCESS RESPONSE1: \(response)")
                }
            case .failure(let encodingError):

                print("ERROR RESPONSE: \(encodingError)")
            }
        })
    }

之前的错误是 403 forbidden ,但我已经将“https://www.googleapis.com/auth/youtube.force-ssl”添加到范围,现在我得到了 400。

如有任何帮助,我们将不胜感激。谢谢。

return 数据中的这一行很有趣:

Required parameter: part

它告诉您需要一个名为 part 的参数,而您没有在调用中包含该参数,所以这就是您收到错误的原因。

如果您查看 Youtube API description,您可以看到 part 参数的说明。

例如

The part parameter serves two purposes in this operation. It identifies the properties that the write operation will set as well as the properties that the API response will include.

The following list contains the part names that you can include in the parameter value and the quota cost for each part:

  • contentDetails: 2

  • fileDetails: 1

  • id: 0

  • liveStreamingDetails: 2

  • localizations: 2

  • player: 0

  • processingDetails: 1

  • recordingDetails: 2

  • snippet: 2

  • statistics: 2

  • status: 2

  • suggestions: 1

  • topicDetails: 2

好的..这可能没有多大意义,但幸运的是还有一些例子。

Here 是 link 使用 python 的示例。

如果你看那个例子,有趣的部分(没有双关语)就在这里:

# Call the API's videos.insert method to create and upload the video.
insert_request = youtube.videos().insert(
    part=",".join(body.keys()),

好的,所以我们加入了一个名为 body 的键,最终成为 part 参数的值。

下一个问题...body 是如何定义的?

body=dict(
  snippet=dict(
    title=options.title,
    description=options.description,
    tags=tags,
    categoryId=options.category
  ),
  status=dict(
    privacyStatus=options.privacyStatus
  )
)

所以...body 的键必须是 snippetstatus。如果您查看上面我的回答中“以下列表包含可以包含在参数值中的部件名称”所描述的列表中提到的有效值,您会看到 snippetstatus 那里,所以他们似乎有效。

所以简而言之,您应该在向 YouTube API 发出 POST 请求时添加 part 参数。该参数的值可能是 snippet, status,但您可能应该仔细阅读它的确切含义以及是否应该使用其他参数。

希望对你有所帮助。