带有--data-binary的VB.NET PUT请求将不起作用。卷曲地工作。 401未经授权

问题描述 投票:0回答:1

我有一个问题,我为此感到挣扎。我正在尝试通过其API将音频上传到在线服务。

我有一个curl命令,但是在VB.net中不起作用(如果有帮助,则在Community 2017中可用)。我知道MS不再使用VB,但是我想将此新的上传功能合并到我多年来编写的当前程序中。

问题是该示例适用于curl :(通常,您也将具有programId,clipID和Auth API密钥(SPECIALLONGKEY),但我将它们隐藏了)

    curl "https://api.omnystudio.com/v0/programs/{programId}/clips/{clipId}/audio" --request PUT --data-binary "@birthday.mp3" --header "Accept:application/json" --header "Authorization:OmnyToken SPECIALLONGKEY"

但是,当我在VB.NET中尝试相同的操作时,收到“ 401未经授权”响应。

    Dim httpClient As New Http.HttpClient()
    Dim request = New Http.HttpRequestMessage(New Http.HttpMethod("PUT"), "https://api.omnystudio.com/v0/programs/{programId}/clips/{clipId}/audio")
    request.Headers.Add("Accept", "application/json")
    request.Headers.Add("Accept-Encording", "gzip, deflate, br")
    request.Headers.Add("Authorization", "OmnyToken SPECIALLONGKEY")

    request.Content = New StringContent("C:\test\birthday.mp3")
    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded")

    Dim response = httpClient.SendAsync(request).Result

    Debug.Print(request.ToString)
    Debug.Print(response.ToString)

我得到这样的答复:

Method: PUT, RequestUri: 'https://api.omnystudio.com/v0/programs/{programId}/clips/{clipId}/audio', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Accept: application/json
  Accept-Encording: gzip, deflate, br
  Authorization: OmnyToken SPECIALLONGKEY
  Content-Type: application/x-www-form-urlencoded
  Content-Length: 50
}
StatusCode: 401, ReasonPhrase: 'Unauthorized. Provide a valid Authentication header', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: keep-alive
  CF-Cache-Status: DYNAMIC
  Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
  CF-RAY: 5841cc895a0fda5e-SYD
  Cache-Control: no-cache, private
  Date: Wed, 15 Apr 2020 01:12:34 GMT
  Server: cloudflare
  WWW-Authenticate: OmnyToken
  Content-Length: 0
}

API文档对我没有多大帮助,因为它只是说:“剪辑的音频可以作为二进制请求正文提供]

我跑Fiddler看看正在发生什么,并且似乎一切正常,正在发送正确的标头,等等。只是想知道我做错了什么还是做错了,或者,我是否应该以编写批处理文件的方式,然后在每次需要发生这种特定情况时让程序执行它们。谢谢。

vb.net api curl put
1个回答
0
投票

在从开发人员那里得到一些帮助之后,似乎有两个问题。最大的问题是(并且仍然是)重定向问题,该问题仍在发生,但是由于存在ContentType问题,现在正在上传音频。

虽然仍然发生重定向,但可以通过轮询API来了解剪辑的上载何时起作用或不起作用以及何时完成处理,从而克服了这一难题。

这是更新的vb.net示例代码:

    Dim OmnyProgramId As String = ""
    Dim OmnyClipId As String = ""
    Dim OmnyToken As String = "SPECIALLONGKEY"

    ' This creates a 303 "See Other" Error
    Dim myHandler As HttpClientHandler = New HttpClientHandler()
    myHandler.AllowAutoRedirect = False
    Dim httpClient As New Http.HttpClient(myHandler)

    ' Original
    ' This creates a redirect 303 "StatusCode: 401, ReasonPhrase: 'Unauthorized. Provide a valid Authentication header" Error
    'Dim httpClient As New Http.HttpClient() 

    Dim request = New Http.HttpRequestMessage(New Http.HttpMethod("PUT"), "https://api.omnystudio.com/v0/programs/" & OmnyProgramId & "/clips/" & OmnyClipId & "/audio")
    request.Headers.Add("Accept", "application/json")
    request.Headers.Add("Accept-Encording", "gzip, deflate, br")
    request.Headers.Add("Authorization", "OmnyToken " & OmnyToken)

    'easily swap between two test files.
    'Dim filename As String = "C:\test\bahamas.mp3"
    Dim filename As String = "C:\test\birthday.mp3"

    Dim dataBytes() As Byte = IO.File.ReadAllBytes(filename)
    Dim dataStream = New MemoryStream(dataBytes)
    request.Content = New StreamContent(dataStream)

    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/mpeg")

    ' Do the upload
    Dim response = httpClient.SendAsync(request).Result

    ' See the output
    Debug.Print("::")
    Debug.Print(request.ToString)
    Debug.Print("::")
    Debug.Print(response.ToString)

我仍然收到难以响应的标题响应,至少上传了音频。这些是标头响应:(再次检查程序,剪辑和api键)

::
Method: PUT, RequestUri: 'https://api.omnystudio.com/v0/programs/{program}/clips/{clip}/audio', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Accept: application/json
  Accept-Encording: gzip, deflate, br
  Authorization: OmnyToken SPECIALLONGKEY
  Content-Type: audio/mpeg
  Content-Length: 1392706
}
::
StatusCode: 303, ReasonPhrase: 'See Other', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: keep-alive
  X-RateLimit-Limit: 10
  X-RateLimit-Remaining: 9
  X-RateLimit-Reset: 10
  CF-Cache-Status: DYNAMIC
  Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
  CF-RAY: 58436ad7eec8fd2e-SYD
  Cache-Control: no-cache, private
  Date: Wed, 15 Apr 2020 05:55:26 GMT
  Location: https://api.omnystudio.com/v0/programs/{program}/clips/{clip}
  Set-Cookie: __cfduid=d5d4e3ab37736930123; expires=Fri, 15-May-20 05:55:23 GMT; path=/; domain=.omnystudio.com; HttpOnly; SameSite=Lax
  Server: cloudflare
  Content-Length: 0
}

[希望这对某人有所帮助,考虑到这些天C#.NET中几乎所有内容,并且VB.NET示例正在沿dodo的方向发展。

© www.soinside.com 2019 - 2024. All rights reserved.