如何使用Restfb通过Java访问“我/帐户”访问令牌?

时间:2019-01-28 05:30:00

标签: java facebook-graph-api facebook-access-token restfb

我正在尝试通过restfb访问我所管理的Facebook页面的详细信息。我可以获取用户详细信息,但是当我尝试从“我/帐户”获取详细信息时,它只会返回null。

我尝试使用不同的类型和类,但仍然无法显示我的需求。但是,使用Facebook Graph API工具可以正常工作

private void authUser(ActionEvent事件){

    String appId = "(My App ID)";
    String domain = "https://www.google.com";
    String authUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&client_id="+appId+"&redirect_uri="+domain+"&scope=ads_management, ads_read, business_management, email, groups_access_member_info, leads_retrieval, manage_pages, pages_manage_cta, pages_manage_instant_articles, pages_messaging, pages_messaging_phone_number, pages_messaging_subscriptions, pages_show_list, publish_pages, publish_to_groups, publish_video, read_audience_network_insights, read_insights, read_page_mailboxes, user_age_range, user_birthday, user_events, user_friends, user_gender, user_hometown, user_likes, user_link, user_location, user_photos, user_posts, user_status, user_tagged_places, user_videos";

    System.setProperty("webdriver.chrome.driver", "chromedriver");

    WebDriver driver = new ChromeDriver();
    driver.get(authUrl);
    String accessToken;
    String accessToken2 = null;
    while(true){
        if(!driver.getCurrentUrl().contains("facebook")){
            String url = driver.getCurrentUrl();
            accessToken = url.replaceAll(".*#access_token=(.+)&.*","$1");
            int indexOfLast = accessToken.lastIndexOf("&");
            if(indexOfLast >=0) accessToken2 = accessToken.substring(0, indexOfLast);
            System.out.println(accessToken);
            System.out.println(accessToken2);

            driver.quit();
            driver = null;

            FacebookClient fbClient = new DefaultFacebookClient(accessToken2, com.restfb.Version.VERSION_3_2);

            User user = fbClient.fetchObject("me", User.class, Parameter.with("fields", "name, email"));

            User account = fbClient.fetchObject("me/accounts", User.class, Parameter.with("fields", "name"));

            name.setText(user.getName());
            id.setText(user.getId());

            if (user.getEmail() == null) {
                email.setText("Unable to show email");
            } else {
                email.setText(user.getEmail());
            }


            if (account.getName()== null) {
                System.out.println("Null");
            } else {
                System.out.println(account.getName());
            } 
        }
    } 
}

0 个答案:

没有答案