VB.NET 使用 --data-binary 的 PUT 请求将不起作用。在卷曲中工作。 401未经授权
VB.NET PUT Request with --data-binary won't work. Works in curl. 401 Unauthorized
我遇到了一点问题,我正在努力解决这个问题。我正在尝试通过他们的 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 文档对我帮助不大,因为它只是说:"The clip's audio can either be provided as a binary request body"
我 运行 Fiddler 看看发生了什么,一切似乎都很好,正确的 headers 正在发送等等。只是想知道我是否做错了,或者,如果我应该只采用编写批处理文件的方式,然后在每次需要发生这种特定情况时让程序执行它们。谢谢。
在得到开发人员的一点帮助后,似乎有 2 个问题。
最大的问题是(现在仍然是)重定向问题,该问题仍在发生,但音频正在上传,因为存在 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)
我仍然收到 header 难以回复的回复,至少音频可以上传。
这些是 header 回复:(程序、剪辑和 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 示例正在走向成功,我希望这对某人有所帮助。
我遇到了一点问题,我正在努力解决这个问题。我正在尝试通过他们的 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 文档对我帮助不大,因为它只是说:"The clip's audio can either be provided as a binary request body"
我 运行 Fiddler 看看发生了什么,一切似乎都很好,正确的 headers 正在发送等等。只是想知道我是否做错了,或者,如果我应该只采用编写批处理文件的方式,然后在每次需要发生这种特定情况时让程序执行它们。谢谢。
在得到开发人员的一点帮助后,似乎有 2 个问题。
最大的问题是(现在仍然是)重定向问题,该问题仍在发生,但音频正在上传,因为存在 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)
我仍然收到 header 难以回复的回复,至少音频可以上传。 这些是 header 回复:(程序、剪辑和 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 示例正在走向成功,我希望这对某人有所帮助。