Java + RestFB API:获得新的页面访问令牌而不会弄乱AppID,appSecret [英] Java + RestFB API: Getting fresh Page Access Token without messing with AppID, appSecret

查看:214
本文介绍了Java + RestFB API:获得新的页面访问令牌而不会弄乱AppID,appSecret的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的程序,一次在页面的墙上张贴5-10个状态.该页面上的帖子必须在页面名称下完成.

I am trying to make a simple program that posts 5-10 statuses, at a time, on a page's wall. The post to the page will have to be done under the name of the page.

我已经阅读了许多写得不好的Facebook开发人员文档,而我正陷入混乱的境地,我什至不知道要问什么问题.我就是她.

I've read tons of badly written Facebook Developers documentation and I'm reaching the point of confusion where I don't even know what questions to ask. So her I am.

通过这种方法,我手动获得了页面访问令牌:

I manually got the Page Access token manually, by this method:

  1. 转到 https://developers.facebook.com/tools/explorer
  2. 在GET请求表中,填写我/帐户
  3. 您将获得基本用户数据的Javascript表示形式.找到您想要的页面.
  4. 请注意access_token和id字段,我们将在下面的代码中使用它们.
  1. Go to https://developers.facebook.com/tools/explorer
  2. At the GET request form, down there, fill in me/accounts
  3. You'll get a Javascript representation of your basic user data. Find the page you want.
  4. Note the access_token and id fields, we're going to use them in the code below.

因此,手动获取页面访问令牌后(当然还有页面的ID)

Thus, after getting the page Access token manually (And the ID of the page, of course)

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.exception.FacebookException;
import com.restfb.types.FacebookType;
import com.restfb.types.Page;
import com.restfb.types.User;

/**
 *
 * @author dsfounis
 */
public class FacebookConnector {

    /* Variables */
    private final String pageAccessToken = "GOT_THIS_FROM_THE_METHOD_ABOVE";
    private final String pageID = "THIS_TOO";
    private FacebookClient fbClient;
    private User myuser = null;    //Store references to myr user and page
    private Page mypage = null;    //for later use. In this question's context, these
                                   //references are useless.
    private int counter = 0;

    public FacebookConnector() {
        try {

            fbClient = new DefaultFacebookClient(pageAccessToken);
            myuser = fbClient.fetchObject("me", User.class);
            mypage = fbClient.fetchObject(pageID, Page.class);
            counter = 0;
        } catch (FacebookException ex) {     //So that you can see what went wrong
            ex.printStackTrace(System.err);  //in case you did anything incorrectly
        }
    }

    public void makeTestPost() {
        fbClient.publish(pageID + "/feed", FacebookType.class, Parameter.with("message", Integer.toString(counter) + ": Hello, facebook World!"));
        counter++;
    }

}


问题:

上面的代码有效.问题是,它暂时有效.我获得的page access token的有效时间为一小时,并且每次运行程序时,我都需要手动完成获取它的过程.如果我将流程的某些方面保留为手动,那么自动化流程的意义何在?


The problem:

The code above works. The thing is, it works temporarily. The page access token that I get has an expiration time of one hour, and I need to manually go through the process of obtaining it, every time that I run the program. What is the point of automating a process if I keep some aspects of it manual?

所以我必须问你:我可以通过编程方式完成上述过程,并在程序启动时获得新的page access token吗?

So I have to ask you: Can I do the process above programmatically, and obtain a fresh page access token at program launch?

也许我可以使用更好的API来做像一样简单的事情,每天只将几件事张贴在Page的墙上吗?

Can I, maybe, use a better API to do something as simple as just post a couple of things on a Page's wall, every day?

我的应用程序是一个控制台程序,我想避免实现不必要的登录,即使您告诉我它是必需的,也将是我必须经历的麻烦.

My application is a console one, and I would like to stay away from implementing needless Logins, even though if you tell me that it is needed, it's going to be a bother I'll have to go through.

请注意::我已经在Facebook Developers中注册了该应用程序,尽管它只是一个基本应用程序.为了获得更多权限,我需要显示Facebook登录实施的证明,正如我在标题中所述,这是我必须避免的事情.

As a note: I've got the application registered in Facebook Developers, albeit only as a basic app. To get more permissions, I need to show proof of Facebook Login implementation, and as I say in the title, it's something I'll have to avoid.

推荐答案

没有自动过程来获取访问令牌.如果有的话,它将破坏OAuth流程的全部目的.对于宠物项目和测试,可以使用Graph API Explorer,但对于涉及用户的公共应用程序,则必须由用户手动选择登录对话框.

There is no automatic process to obtain an access token. If there was, it will defeat the whole purpose of the OAuth flow. For pet projects and tests it's okay to use the Graph API Explorer but for public applications involving users it is mandatory that the user manually selects the login dialog.

在当前情况下,您可以使用此处提到的方法扩展用户令牌 https://developers.facebook.com/docs/roadmap/completed-changes/offline-access-removal/

Under your current scenario you can extend the user token using the method mentioned here https://developers.facebook.com/docs/roadmap/completed-changes/offline-access-removal/

方案5:页面访问令牌

Scenario 5: Page Access Tokens

当用户授予某个应用程序"manage_pages"权限时,该应用程序将能够 获取用户管理的页面的页面访问令牌 查询[用户ID]/帐户Graph API端点.随着迁移 使用短期用户访问令牌查询时启用 端点,获得的页面访问令牌也是短暂的.

When a user grants an app the manage_pages permission, the app is able to obtain page access tokens for pages that the user administers by querying the [User ID]/accounts Graph API endpoint. With the migration enabled, when using a short-lived user access token to query this endpoint, the page access tokens obtained are short-lived as well.

将短期用户访问令牌交换为长期访问 使用端点和之前解释的步骤的令牌.

Exchange the short-lived user access token for a long-lived access token using the endpoint and steps explained earlier.

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN

通过使用 长期的用户访问令牌,查询[用户ID]/帐户端点 现在将提供不会过期的页面访问令牌 用户管理.当查询过期时,这也将适用 通过不赞成使用的offline_access获得的用户访问令牌 许可.

By using a long-lived user access token, querying the [User ID]/accounts endpoint will now provide page access tokens that do not expire for pages that a user manages. This will also apply when querying with a non-expiring user access token obtained through the deprecated offline_access permission.

仅由应用程序所有者使用的简单程序不需要Facebook的批准.

A simple program used only by the owner of the application does not need approval from Facebook.

例如 https://www.facebook.com/phwdbot

这篇关于Java + RestFB API:获得新的页面访问令牌而不会弄乱AppID,appSecret的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆