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



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

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;
    }
}

相关内容

  • 没有找到相关文章

最新更新