【问题标题】:Error uploading video via Graph API on Android在 Android 上通过 Graph API 上传视频时出错
【发布时间】:2016-08-04 13:23:38
【问题描述】:

我正在使用 Graph API 开发概念验证 Android 应用程序。我正在尝试将视频上传到我的 Facebook 应用的相册。

我使用这部分代码来做到这一点:

String dataName = new File(getRealPathFromURI(this,data.getData())).getName();
            Toast.makeText(this,dataName,Toast.LENGTH_LONG).show();
            params.putString("filename", new File(getRealPathFromURI(this,data.getData())).getName());
            params.putByteArray("source",k);
            /* make the API call */
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/"+sProfile.getId()+"/videos",
                    params,
                    HttpMethod.POST,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                            TextView textView = (TextView) findViewById(R.id.textView2);
                                textView.setText(response.toString()+" "+k.length);
                            Log.v("Response:",response.toString());
                        }
                    }
            ).executeAsync();

尽管这是 Android 相当于 Facebook 的 suggested method 并且我正在尝试上传工作的 .mp4 文件,但我收到此错误。

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400,errorCode: 352, errorType: OAuthException, errorMessage: Sorry, the video file you selected is in a format that we don't support.}}

在我看来,Android Facebook SDK 中的 GraphRequest.java 中有一个错误,导致要上传的视频的文件名设置为“源”。

在这里,我们在 GraphRequest.java 的第 2166 和 2167 行看到:

public void writeBytes(String key, byte[] bytes) throws IOException {
        writeContentDisposition(key, key, "content/unknown");

和 writeContentDisposition 的参数在同一个文件中的第 2245-2248 行:

public void writeContentDisposition(
            String name,
            String filename,
            String contentType
    )

如果是params.putByteArray("source",k);,应该将文件名设置为“源”,没有文件扩展名供 facebook 使用。

添加params.putString("filename", <FILENAME>); 根本不起作用,因为这不会更改错误分配给source 的文件名。

Graph API 2.5 video reference 我们可以阅读以下内容:

当包含一个源参数时,您应该包含一个带有正确扩展名的文件名,以表明文件容器的类型。

有趣的是,2.6 video reference 中没有这样的内容。

我的问题是:

  1. GraphRequest.java 中真的存在错误吗?还是 2.6 视频参考正确,文件名不再重要?
  2. 如果存在错误,当我通过 build.gradle 依赖项使用 API 时如何更改类?
  3. 如果没有错误,我的上传脚本有什么问题,如何防止 Facebook 给我格式错误响应?

【问题讨论】:

    标签: android facebook-graph-api video facebook-android-sdk uploading


    【解决方案1】:

    所以,Facebook Android SDK 中似乎确实存在错误。

    作为一种解决方法,我使用了Android Asynchronous Http Client,我使用它创建了以下方法:

    void uploadVideo(String name, InputStream is, String fileName){
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams rp = new RequestParams();
        rp.put("access_token",AccessToken.getCurrentAccessToken().getToken());
        rp.put("name",name);
        rp.put("source",is,fileName);
        client.post(this, "https://graph-video.facebook.com/me/videos", rp, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //something...
            }
    
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //something...
            }
        });
    }
    

    一切都按预期进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2015-03-13
      • 2014-09-27
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      相关资源
      最近更新 更多