Facebook Graph Api Android sharing image on a page

时间:2016-03-14 07:57:42

标签: android facebook

I want to share an image on a Facebook page of mine. But I couldn't figure out how to do this step by step. And couldn't find step by step guide for this.

This is my code to share an image on a page

Bundle params = new Bundle();

String nameText = name.getText().toString();
String tags = engine.implodeTags(tagsList);
String textText = text.getText().toString();
params.putString("caption", nameText + "\n\n" + textText + "\n\n" + tags);

params.putString("url", imagesList.get(mainImageSelected).getImageUrl());

params.putString("access_token", "{access token here}");

new GraphRequest(
    AccessToken.getCurrentAccessToken(),
    "/{page_id here}/photos",
    params,
    HttpMethod.POST,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            if(response.getJSONObject()!=null) {
                Log.d("qwe", response.getJSONObject().toString());
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "Shared on facebook", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                    }
                });
            }
            else{
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "Error", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                    }
                });
            }
        }
    }
).executeAsync();

It works if I put access token by hand. I am getting access token here https://developers.facebook.com/tools/explorer。 但是一段时间后,这个访问令牌不再起作用了。所以我需要获得新的访问令牌。

如何从Android本身获取PAGE访问令牌?通过facebook sdk的用户登录按钮?

https://developers.facebook.com/docs/facebook-login/access-tokens

请帮忙。

2 个答案:

答案 0 :(得分:0)

如果您使用Facebook SDK轻松实现这一目标。请阅读https://developers.facebook.com/docs/sharing/android

答案 1 :(得分:0)

您需要获取永久访问令牌才能在您的页面上共享图像。要做到这一点,你需要按照这里写的步骤。 facebook: permanent Page Access Token?