Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload video on youtube using access token and api in android?

I build web and mobile app and they are synchronized. So, after I login and authorize google account to get access token, refresh token, expires to upload video on youtube and store in database. And on mobile, I get access token, refresh token, expires ria api that I write and mobile app use access token to upload video without login google account to get access token. If access token is expired, I will use refresh token to re-generate access token. Now, I have done in iOS, but in Android I still can't do it.

like image 722
user1916184 Avatar asked Apr 03 '15 10:04

user1916184


People also ask

Can you upload videos with YouTube API?

Stay organized with collections Save and categorize content based on your preferences. This guide provides and explains a Python script that uploads a YouTube video using the YouTube Data API. The code uses the Google APIs Client Library for Python.

What is an access token for YouTube?

The access token lets the application authorize requests on the user's behalf, and the refresh token lets the application retrieve a new access token when the original access token expires.


1 Answers

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;
    }
}
like image 83
Rajesh Sharma Avatar answered Oct 12 '22 15:10

Rajesh Sharma