如何在Android上使用访问令牌和api在youtube上传视频?

时间:2015-04-03 10:20:07

标签: android google-api google-api-java-client android-youtube-api

我构建了网络和移动应用,并且它们是同步的。因此,在我登录并授权谷歌帐户获取访问令牌后,刷新令牌,到期以在youtube上传视频并存储在数据库中。在移动设备上,我获得访问令牌,刷新令牌,到期我写的ria api和移动应用程序使用访问令牌上传视频而无需登录谷歌帐户获取访问令牌。如果访问令牌过期,我将使用刷新令牌重新生成访问令牌。现在,我已经在iOS中完成了,但在Android中我仍然无法做到。

1 个答案:

答案 0 :(得分:1)

public class UploadService extends AsyncTask<Void,Void,String>
{
    String base64_video="";
    String tokenValue="";
     ProgressDialog uploadDialog = new ProgressDialog(FinalStandardActivity.this);


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        base64_video=convertToBase64(_newVideoPath);

        uploadDialog.setMessage("Uploading...");
        uploadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        uploadDialog.setCancelable(false);
        uploadDialog.show();

    }

    @Override
    protected String doInBackground(Void... params) {

        File file=new File(_newVideoPath);

        loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
        tokenValue=loginPreferences.getString("googleToken","");

        if(tokenValue.contentEquals(""))
        {

        }
        else
        {
           JSONObject snippet=new JSONObject();

            //VideoSnippet snippet = new VideoSnippet();
            VideoStatus status = new VideoStatus();




            try
            {

                snippet.put("categoryId", "22");
                snippet.put("description", "Description of uploaded video.");
                snippet.put("title", "Test video upload");
                status.set("privacyStatus", "private");


            }
            catch (Exception e)
            {
                e.printStackTrace();
            }



            // .addFormDataPart("snippet","snippet",RequestBody.create(MediaType.parse("application/json; charset=utf-8"),snippet.toString()))



            OkHttpClient client = new OkHttpClient();
            RequestBody formBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("snippet","snippet",
                            RequestBody.create(MediaType.parse("application/json; charset=utf-8"),snippet.toString()))
                    .addFormDataPart("videoFile", file.getName(),
                            RequestBody.create(MediaType.parse("video/*"), file))
                    .build();
            //   let metadata = "{'snippet':{'title' : 'title', 'description': 'description'}}".data(using: .utf8, allowLossyConversion: false)!

            //MediaType.parse("application/json; charset=utf-8")



            //UPLOAD_URL=UPLOAD_URL+"?part="+snippet.toString();

            Request request = new Request.Builder()
                    .addHeader("Authorization","Bearer "+tokenValue)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("cache-control", "no-cache")
                    .url(UPLOAD_URL)
                    .post(formBody)
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e)
                {
                    Log.e(TAG, e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException
                {
                    Log.e(TAG,response.body().toString());
                    uploadDialog.dismiss();
                }
            });

        }
        return null;
    }
}