在Android应用程序中集成youtube like按钮

时间:2017-03-04 05:22:12

标签: android youtube-api youtube-data-api

我想在我的Android应用中集成youtube like button。通过本教程引用Videos: rate

我发现这个api链接https://www.googleapis.com/youtube/v3/videos/rate可以用来做到这一点。任何人都可以告诉我如何传递特定视频ID以及此api链接

1 个答案:

答案 0 :(得分:0)

将此代码用于youtube视频....

// getPostLikeBtn(创建此方法。)

private void getPostLikeBtn(final String rating) {
    String tag_json_obj = "recipeLike";


    final SpotsDialog spotsDialog = new SpotsDialog(context);

    spotsDialog.show();
    spotsDialog.setMessage("Loading...");

    StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST,
            "https://www.googleapis.com/youtube/v3/videos/rate",

            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    spotsDialog.dismiss();


                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            try {
                spotsDialog.dismiss();
                try {
                    if (error.networkResponse.data != null) {
                        try {
                            String body = new String(error.networkResponse.data, "UTF-8");
                            Log.e("errorLike", body);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }

                    spotsDialog.dismiss();
                    Toast.makeText(context, getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Toast.makeText(context, getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
            // hide the progress dialog

        }
    }) {

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            int mStatusCode = response.statusCode;
            Log.e("mStatusCode", "" + mStatusCode);
            if (mStatusCode == 204) {
                Toast.makeText(context, "Successfully updated", Toast.LENGTH_SHORT).show();
                getLikeShare();

            } else {
                Toast.makeText(context, getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
            }
            return super.parseNetworkResponse(response);
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            SharedPreferences prefs = getSharedPreferences("GOOGLE_TOKEN", MODE_PRIVATE);
            String token = prefs.getString("token", null);
            params.put("Authorization", "Bearer " + token);
            return params;
        }

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> jsonObject = new HashMap<>();
            try {
                jsonObject.put("id", "Video id");
                jsonObject.put("rating", rating);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.e("jsonObject", "" + jsonObject);
            return jsonObject;
        }
    };


    AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);

}

我希望这对你有用..