从Wp8直接上传Youtube的POST请求

时间:2013-09-29 11:15:08

标签: c# windows-phone-8 youtube-api windows-phone youtube.net-api

在谷歌上搜索从我的Windows Phone 8应用程序上传视频的解决方案时,我被带到了YouTube API v2.0 – Direct Uploading。我按照文章编写如下。

    private void UploadVideoYoutube(byte[] byteArr, string isoVideoFileName)
    {
        Uri uri = new Uri("http://uploads.gdata.youtube.com/feeds/api/users/default/uploads");
        Dictionary<string, string> post_params = new Dictionary<string, string>();
        Dictionary<string, string> extra_headers = new Dictionary<string, string>();
        RESTSuccessCallback success_callback = new RESTSuccessCallback(Success);
        RESTErrorCallback error_callback = new RESTErrorCallback(Error);
        try
        {
            HttpWebRequest request = WebRequest.CreateHttp(uri);

            //we could move the content-type into a function argument too.
            //request.ContentType = "application/atom+xml; charset=UTF-8";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "video/mp4";
            request.Method = "POST";

            //provide parameters
            post_params.Add("Authorization", "<ClientID>");

            //this might be helpful for APIs that require setting custom headers...
            if (extra_headers != null)
                try
                {
                    foreach (String header in extra_headers.Keys)
                        request.Headers[header] = extra_headers[header];
                }
                catch (Exception) { }

            request.BeginGetRequestStream((IAsyncResult result) =>
            {
                HttpWebRequest preq = result.AsyncState as HttpWebRequest;
                if (preq != null)
                {
                    Stream postStream = preq.EndGetRequestStream(result);

                    //allow for dynamic spec of post body
                    StringBuilder postParamBuilder = new StringBuilder();
                    if (post_params != null)
                        foreach (String key in post_params.Keys)
                            postParamBuilder.Append(String.Format("{0}={1}", key, post_params[key]));

                    Byte[] requestByteArray = Encoding.UTF8.GetBytes(postParamBuilder.ToString());

                    //guess one could just accept a byte[] [via function argument] for arbitrary data types - images, audio,...
                    postStream.Write(requestByteArray, 0, requestByteArray.Length);
                    postStream.Close();

                    preq.BeginGetResponse((IAsyncResult final_result) =>
                    {
                        HttpWebRequest req = final_result.AsyncState as HttpWebRequest;
                        if (req != null)
                        {
                            try
                            {
                                //we call the success callback as long as we get a response stream
                                WebResponse response = req.EndGetResponse(final_result);
                                success_callback(response.GetResponseStream());
                            }
                            catch (WebException e)
                            {
                                //otherwise call the error/failure callback
                                error_callback(e.Message);
                                return;
                            }
                        }
                    }, preq);
                }
            }, request);
        }
        catch (Exception ex)
        {
            AppHelper.ErrorOccured(ex);
        }
    }

但它返回以下错误,请指导我。 如果有任何针对Windows手机的youtube库,请告知我们。 感谢

  

远程服务器返回错误:NotFound。   enter image description here

0 个答案:

没有答案