如何在图中使用 facebook 访问令牌作为字符串 api

How to use facebook access token as a string in graph api

实际上我想 post 在我的页面上作为页面而不是作为用户在 android 应用程序中使用 facebook sdk 但问题是页面 "AccessToken" 我从图形请求中检索在字符串中,现在我不能在其他图形请求中使用它。它说 String 无法转换为 AccessToken。

这是我的代码。

  package com.mak.masimmak.pageshare;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.Toast;

        import com.facebook.AccessToken;
        import com.facebook.GraphRequest;
        import com.facebook.GraphResponse;
        import com.facebook.HttpMethod;

        import org.json.JSONException;
        import org.json.JSONObject;


        public class sharePage extends AppCompatActivity {
         Button shareButton;
            public String PageId;
            String PageAccessToken;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_share_page);

                shareButton = (Button) findViewById(R.id.shareButton);



                shareButton.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View view) {

                        if(getIntent().getExtras() != null) {
                            Bundle extras = getIntent().getExtras();
                            PageId = extras.getString("PageId");

                            /* make the API call */
                            new GraphRequest(
                                    AccessToken.getCurrentAccessToken(),
                                    "/"+PageId+"?fields=access_token",
                                    null,
                                    HttpMethod.GET,
                                    new GraphRequest.Callback() {
                                        public void onCompleted(GraphResponse response) {
                                             /* handle the result */


                                            try {
                                            JSONObject data = response.getJSONObject();
                                             PageAccessToken = data.getString("access_token");

                                                PostOnPage();



                                            } catch (JSONException e) {
                                                Toast.makeText(sharePage.this, e.toString(), Toast.LENGTH_LONG).show();
                                            }


                                        }
                                    }
                            ).executeAsync();




                        }
                    }

                });
            }

            public void PostOnPage(){

                Bundle params = new Bundle();
                params.putString("message", "This is a test message");
        /* make the API call */
                new GraphRequest(
  /* Problem is here the PageAccessToken is in String How to use this string token here? */
                        (AccessToken) PageAccessToken,
                        "/"+PageId+"/feed",
                        params,
                        HttpMethod.POST,
                        new GraphRequest.Callback() {
                            public void onCompleted(GraphResponse response) {
                    /* handle the result */
                            }
                        }
                ).executeAsync();

            }
        }

我在 google 中搜索问题,我发现了一些东西..

public AccessToken(String accessToken, String applicationId, String userId, Collection permissions, Collection declinedPermissions, AccessTokenSource accessTokenSource, Date expirationTime, Date lastRefreshTime)

但我不知道如何在我的代码中使用或实现它。

我找到了我自己的问题的解决方案。 您可以使用这行代码将 AccessToken 从字符串格式转换为 Facebook AccessToken 格式。

AccessToken PageAT = new AccessToken(PageAccessToken, AccessToken.getCurrentAccessToken().getApplicationId(), AccessToken.getCurrentAccessToken().getUserId(), AccessToken.getCurrentAccessToken().getPermissions(), null, AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, AccessToken.getCurrentAccessToken().getExpires(), null);

那么这个 PageAT 将在您的新图形请求中正常工作..