如何在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 basehttps://www.googleapis.comtoopen(), but you need to include the full endpoint path here instead of trying to set it via a header. - Invalid
POSTheader: The linesetRequestHeader("POST", "...")is not valid—HTTP method is specified in theopen()call, not as a request header. - Missing
Content-Typeheader: Since you’re sending a JSON payload for the video snippet, you need to explicitly setContent-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.uploadscope—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
PUTrequest to thesessionUrlreturned, sending the binary video data with the exactContent-Lengthyou specified earlier. - XMLHTTP Version: Use
MSXML2.ServerXMLHTTP.6.0instead 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
StatusandStatusTextproperties to catch network issues or API errors early.
内容的提问来源于stack exchange,提问作者Already
相关产品推荐
相关产品推荐

