API文档地址:
https://open.douyin.com/platform/doc/OpenAPI-video-create
调用接口:
接口参数:
平台已申请的权限:
问题描述:
在调用 https://open.douyin.com/video/upload/接口是总数返回参数不合法,用该接口调用时所使用的open_id和access_token调用其他接口(如:/video/list/)都可返回调用成功数据,可以肯定的是,open_id和access_token参数没有问题,接口权限 (Scope: “video.create”)在开放平台已经申请并审核通过,在扫码授权时,也已经传如了Scope权限。
调用API返回结果如下:
{"data":{"description":"参数不合法","error_code":2100005},"extra":{"logid":"202003211443340100140470891DF1B821","now":1584773018222}}
注:接口测试采用多种测试方式都是这个结果:
所采用的测试方式有:
1)C# 程序调用
2)jquery前端直接调用
3) postman调用
C# 实现代码:
var userInfo = _sessionHelper.GetSession();
var url = "https://open.douyin.com/video/upload";
string filePath = @"D:/dy3.mp4";
Dictionary<string, string> dicData = new Dictionary<string, string>();
dicData.Add("open_id", userInfo.data.open_id);
dicData.Add("access_token", userInfo.data.access_token);
Dictionary<string, string> dicFiles = new Dictionary<string, string>();
dicFiles.Add("video", "D:/dy3.mp4");
var res = Util.PostHttpResponse(url, dicData, dicFiles);
PostHttpResponse方法:
public static string PostHttpResponse(string url, Dictionary<string, string> postData, Dictionary<string, string> files)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
//写入文本
if (postData != null && postData.Count > 0)
{
var keys = postData.Keys;
foreach (var key in keys)
{
string strHeader = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n", key);
byte[] strByte = System.Text.Encoding.UTF8.GetBytes(strHeader);
postStream.Write(strByte, 0, strByte.Length);
byte[] value = System.Text.Encoding.UTF8.GetBytes(string.Format("{0}", postData[key]));
postStream.Write(value, 0, value.Length);
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
}
}
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
//写入文件
if (files != null && files.Count > 0)
{
var keys = files.Keys;
foreach (var key in keys)
{
string filePath = files[key];
int pos = filePath.LastIndexOf("\\");
string fileName = filePath.Substring(pos + 1);
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n", key, fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
FileStream fs = new FileStream(files[key], FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
}
}
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); //结束标志
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}