youtube restapi (v3) 集成在 titanium 中用于视频上传
youtube restapi (v3) integration in titanium for video upload
在语言部分我使用 javascript。
youtube v3 提供的代码示例用于 web 视图(包含 html + js),在钛中我没有使用 alloy 我当前的代码是钛经典应用程序。为了使用 rest api,我首先集成了 google oauth,然后在调用上传 api 时抛出了下面提到的错误,除了 upload api other api for channel列表、活动、搜索都运行良好。
抛出的错误有时是 401/400 /500 取决于不同的命中和试用案例。
`var videofile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'phone.mp4');
var args = {
file : videofile.read(),
description : 'test video'
};
var xhr = Ti.Network.createHTTPClient({
onload : function(e) {
try {
console.log(this.responseText);
var data = JSON.parse(this.responseText);
if (params.callback) {
params.callback(data);
} else {
for (var thing in data) {
Ti.API.info("data." + thing + ' = ' + data[thing]);
}
Ti.API.warn("Data received but no callback specified.");
}
} catch(e) {
Titanium.UI.createAlertDialog({
title : 'Error',
message : 'Error accessing data: ' + JSON.stringify(e)
});
Ti.API.error('RESPONSE: ' + JSON.stringify(e));
}
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Titanium.UI.createAlertDialog({
title : 'Error',
message : 'Error accessing data: ' + JSON.stringify(e)
});
Ti.API.error('HTTP: ' + JSON.stringify(e));
},
timeout : 5000
});
console.log(propertyUrl);
var urlString = "https://www.googleapis.com/upload/youtube/v3/videos?" + propertyUrl + "&key="+Titanium.App.Properties.getString('accessTokenGoogle');
xhr.open("post",urlString);
xhr.setRequestHeader("Authorization", "Bearer " + Titanium.App.Properties.getString('accessTokenGoogle'));
ind.show();
xhr.setRequestHeader('Content-Type', 'video/*');
xhr.send(args); `
错误日志
`
[WARN] : TiBaseFile: (KrollRuntimeThread) [8660,8660] Method is not supported org.appcelerator.titanium.io.TiResourceFile : getParent
[INFO] : {"file":{"file":{"hidden":false,"nativePath":"file:///android_asset/Resources/phone.mp4","writable":false,"executable":false,"parent":null,"readonly":true,"directoryListing":[],"size":2076501,"apiName":"Ti.Proxy","name":"phone.mp4","symbolicLink":false,"bubbleParent":true},"nativePath":"file:///android_asset/Resources/phone.mp4","height":0,"length":2076501,"width":0,"mimeType":"video/mp4","apiName":"Ti.Blob","text":null,"type":1,"bubbleParent":true},"description":"test video"}
[INFO] : urlSting ---> https://www.googleapis.com/upload/youtube/v3/videos?part=snippet,contentDetails,fileDetails&key=ya29.OAFXEUm2kfyp39vdNB08nJt9Dswg_HTXBbOKXH97mSok-2rC2M233kEe8tOPrEUOPU8H-NUwZU8edQ
[INFO] : I/System.out: propertyValue:true
[INFO] : I/System.out: [socket][0] connection /74.125.130.95:443;LocalPort=46895(5000)
[INFO] : I/System.out: [CDS]connect[/74.125.130.95:443] tm:5
[INFO] : I/System.out: [socket][/192.168.0.188:46895] connected
[INFO] : I/System.out: [CDS]rx timeout:5000
[ERROR] : NativeCrypto: ssl=0x52982ec0 cert_verify_callback x509_store_ctx=0x54c69ab8 arg=0x0
[ERROR] : NativeCrypto: ssl=0x52982ec0 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
[INFO] : I/System.out: >doSendRequest
[INFO] : I/System.out: <doSendRequest
[INFO] : I/System.out: [CDS]close[46895]
[INFO] : I/System.out: close [socket][/0.0.0.0:46895]
[WARN] : IdleConnectionHandler: Removing a connection that never existed!
[ERROR] : TiHttpClient: (TiHttpClient-1) [1393,10053] HTTP Error (org.apache.http.client.HttpResponseException): Internal Server Error
[ERROR] : TiHttpClient: org.apache.http.client.HttpResponseException: Internal Server Error
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:275)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:219)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1334)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:838)
`
如何使用这个 YouTube API 模块?
找到这个 link 并最终发挥了作用。
在语言部分我使用 javascript。 youtube v3 提供的代码示例用于 web 视图(包含 html + js),在钛中我没有使用 alloy 我当前的代码是钛经典应用程序。为了使用 rest api,我首先集成了 google oauth,然后在调用上传 api 时抛出了下面提到的错误,除了 upload api other api for channel列表、活动、搜索都运行良好。 抛出的错误有时是 401/400 /500 取决于不同的命中和试用案例。
`var videofile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'phone.mp4');
var args = {
file : videofile.read(),
description : 'test video'
};
var xhr = Ti.Network.createHTTPClient({
onload : function(e) {
try {
console.log(this.responseText);
var data = JSON.parse(this.responseText);
if (params.callback) {
params.callback(data);
} else {
for (var thing in data) {
Ti.API.info("data." + thing + ' = ' + data[thing]);
}
Ti.API.warn("Data received but no callback specified.");
}
} catch(e) {
Titanium.UI.createAlertDialog({
title : 'Error',
message : 'Error accessing data: ' + JSON.stringify(e)
});
Ti.API.error('RESPONSE: ' + JSON.stringify(e));
}
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Titanium.UI.createAlertDialog({
title : 'Error',
message : 'Error accessing data: ' + JSON.stringify(e)
});
Ti.API.error('HTTP: ' + JSON.stringify(e));
},
timeout : 5000
});
console.log(propertyUrl);
var urlString = "https://www.googleapis.com/upload/youtube/v3/videos?" + propertyUrl + "&key="+Titanium.App.Properties.getString('accessTokenGoogle');
xhr.open("post",urlString);
xhr.setRequestHeader("Authorization", "Bearer " + Titanium.App.Properties.getString('accessTokenGoogle'));
ind.show();
xhr.setRequestHeader('Content-Type', 'video/*');
xhr.send(args); `
错误日志
`
[WARN] : TiBaseFile: (KrollRuntimeThread) [8660,8660] Method is not supported org.appcelerator.titanium.io.TiResourceFile : getParent
[INFO] : {"file":{"file":{"hidden":false,"nativePath":"file:///android_asset/Resources/phone.mp4","writable":false,"executable":false,"parent":null,"readonly":true,"directoryListing":[],"size":2076501,"apiName":"Ti.Proxy","name":"phone.mp4","symbolicLink":false,"bubbleParent":true},"nativePath":"file:///android_asset/Resources/phone.mp4","height":0,"length":2076501,"width":0,"mimeType":"video/mp4","apiName":"Ti.Blob","text":null,"type":1,"bubbleParent":true},"description":"test video"}
[INFO] : urlSting ---> https://www.googleapis.com/upload/youtube/v3/videos?part=snippet,contentDetails,fileDetails&key=ya29.OAFXEUm2kfyp39vdNB08nJt9Dswg_HTXBbOKXH97mSok-2rC2M233kEe8tOPrEUOPU8H-NUwZU8edQ
[INFO] : I/System.out: propertyValue:true
[INFO] : I/System.out: [socket][0] connection /74.125.130.95:443;LocalPort=46895(5000)
[INFO] : I/System.out: [CDS]connect[/74.125.130.95:443] tm:5
[INFO] : I/System.out: [socket][/192.168.0.188:46895] connected
[INFO] : I/System.out: [CDS]rx timeout:5000
[ERROR] : NativeCrypto: ssl=0x52982ec0 cert_verify_callback x509_store_ctx=0x54c69ab8 arg=0x0
[ERROR] : NativeCrypto: ssl=0x52982ec0 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
[INFO] : I/System.out: >doSendRequest
[INFO] : I/System.out: <doSendRequest
[INFO] : I/System.out: [CDS]close[46895]
[INFO] : I/System.out: close [socket][/0.0.0.0:46895]
[WARN] : IdleConnectionHandler: Removing a connection that never existed!
[ERROR] : TiHttpClient: (TiHttpClient-1) [1393,10053] HTTP Error (org.apache.http.client.HttpResponseException): Internal Server Error
[ERROR] : TiHttpClient: org.apache.http.client.HttpResponseException: Internal Server Error
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:275)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:219)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1334)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:838)
`
如何使用这个 YouTube API 模块?
找到这个 link 并最终发挥了作用。