0

因此,我正在努力将视频发布到Emotion API for video,但我无法得到回复。

我已经能够让它在 Microsoft在线控制台上运行,但是当我尝试使用 (1) JavaScript Ajax 或 (2) Ruby 服务器端代码在我的 Rails 应用程序中实现它时,我总是遇到各种错误。

这是我的代码。起初我尝试使用 Ajax 方式,但我怀疑 API 没有启用 CORS。所以后来我尝试了Ruby,没有成功。

红宝石尝试:

def index
    uri = URI('https://api.projectoxford.ai/emotion/v1.0/recognizeinvideo')
    uri.query = URI.encode_www_form({
    })

    data = File.read("./public/mark_zuck.mov")

    request = Net::HTTP::Post.new(uri.request_uri)
    # Request headers
    request['Ocp-Apim-Subscription-Key'] = 'e0ae8aad4c7f4e33b51d776730cff5a9'
    # Request body
    request.body = data
    request.content_type = "video/mov"

    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        http.request(request)
    end

    puts response.body
end 

这是我的 Ajax 尝试:

    function CallAPI(apiUrl, apiKey){
    console.log("API called");
    $(".loading").css("display", "inline-block");
    $.ajax({
        url: apiUrl,
        beforeSend: function (xhrObj) {
            xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
        },
        type: "POST",
        data: '{"url": "http://localhost:5000/mark_zuck.mov"}',
        processData: false,
        success: function(response){
            console.log("API success");
            ProcessResult(response);
            $(".loading").css("display", "none");
            console.log(response);
        },
        error: function(error){
            console.log("API failed");
            $("#response").text(error.getAllResponseHeaders());
            $(".loading").css("display", "none");
            console.log(error);
        }
    })

是的,我已经重新生成了我的密钥。这只是为了说明我的观点。

4

1 回答 1

0

Content-Type因此,如果它是您发送的二进制文件,您必须设置为 application/octet-stream,就像我一样。

如果你使用 url,你应该设置Content-Type为 application/json 并且 url 必须是公开的。

于 2016-05-11T01:08:49.790 回答