You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在YouTube API v3中通过Classic ASP传递xmlHttp对象

Using Classic ASP with YouTube API v3 Resumable Upload via MSXML2.ServerXMLHTTP

Looking at your code snippet, there are a few key mistakes that are likely causing issues with the YouTube API v3 resumable upload. Let’s break down the fixes and provide a complete working example tailored to Classic ASP.

Common Issues in Your Original Code

  • Incomplete open() URL: You’re passing just the base https://www.googleapis.com to open(), but you need to include the full endpoint path here instead of trying to set it via a header.
  • Invalid POST header: The line setRequestHeader("POST", "...") is not valid—HTTP method is specified in the open() call, not as a request header.
  • Missing Content-Type header: Since you’re sending a JSON payload for the video snippet, you need to explicitly set Content-Type: application/json.
  • Truncated headers: Your code cuts off mid-header setup, so ensure all required headers (like content type for the video file) are included.

Corrected Code Example

Here’s a full, functional snippet for initiating a resumable upload session with YouTube API v3 in Classic ASP:

<%
Dim xmlHttp, URLString, jsonString, ACCESS_TOKEN

' Replace with your valid access token (must have youtube.upload scope)
ACCESS_TOKEN = "YOUR_VALID_ACCESS_TOKEN"

' Build the full resumable upload endpoint
URLString = "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails"

' Create the latest version of the XMLHTTP object for better compatibility
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")

' Initialize synchronous POST request
Call xmlHttp.open("POST", URLString, False)

' Set required request headers
Call xmlHttp.setRequestHeader("Host", "www.googleapis.com")
Call xmlHttp.setRequestHeader("Authorization", "Bearer " & ACCESS_TOKEN)
Call xmlHttp.setRequestHeader("Content-Type", "application/json")
Call xmlHttp.setRequestHeader("X-Upload-Content-Type", "video/mp4") ' Replace with your video's MIME type
Call xmlHttp.setRequestHeader("X-Upload-Content-Length", "123456789") ' Replace with actual video file size in bytes

' Define your video metadata as properly formatted JSON
jsonString = "{" & _
    """snippet"": {" & _
        """title"": ""My Classic ASP Uploaded Video""," & _
        """description"": ""Test video uploaded via Classic ASP and YouTube API v3""," & _
        """tags"": [""classic asp"", ""youtube api"", ""resumable upload"" ]," & _
        """categoryId"": ""22"" " & ' Replace with your desired category ID
    "}," & _
    """status"": {" & _
        """privacyStatus"": ""unlisted"" " & ' Options: public, private, unlisted
    "}" & _
"}"

' Send the metadata payload to initiate the session
Call xmlHttp.send(jsonString)

' Handle the response
If xmlHttp.Status = 200 Then
    ' Extract the resumable session URL from the Location header
    Dim sessionUrl
    sessionUrl = xmlHttp.getResponseHeader("Location")
    Response.Write("Resumable session created successfully! Session URL: <br>" & sessionUrl)
    
    ' Next step: Use this session URL to upload the actual video file via a PUT request
    ' (You'll need to read the video file as binary and send it with the correct Content-Length header)
Else
    ' Output error details for debugging
    Response.Write("Error initiating session: " & xmlHttp.Status & " " & xmlHttp.StatusText & "<br>")
    Response.Write("API Response: <br>" & xmlHttp.responseText)
End If

' Clean up the object
Set xmlHttp = Nothing
%>

Key Notes to Avoid Pitfalls

  • Access Token Scope: Your access token must include the https://www.googleapis.com/auth/youtube.upload scope—without this, you’ll get authorization errors.
  • Resumable Flow: This code only starts the session. To upload the video file, you need to make a PUT request to the sessionUrl returned, sending the binary video data with the exact Content-Length you specified earlier.
  • XMLHTTP Version: Use MSXML2.ServerXMLHTTP.6.0 instead of older versions to ensure compatibility with modern HTTPS endpoints and TLS requirements.
  • JSON Validation: Double-check your JSON string for syntax errors (missing commas, unescaped quotes)—even small mistakes will cause the API to reject the request.
  • Error Handling: Always check the Status and StatusText properties to catch network issues or API errors early.

内容的提问来源于stack exchange,提问作者Already

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:29:52