简体   繁体   English

Facebook4j API:搜索

[英]Facebook4j API : Search

I'm using Facebook4j to get status with a keyword 我正在使用Facebook4j来获取关键字的状态

 facebook4j.conf.ConfigurationBuilder fac = new facebook4j.conf.ConfigurationBuilder();

            fac.setDebugEnabled(true)
              .setOAuthAppId("******")
              .setOAuthAppSecret("********")
              .setOAuthPermissions("email,publish_stream,...");
            FacebookFactory ff = new FacebookFactory(fac.build());
           facebook = ff.getInstance();
           new Thread(new Runnable() { 
               @Override
               public void run() {
                   try {
                       search();
                   }
                   catch (Exception e) {
                    // TODO: handle exception
                       System.out.println(e +" ERROOOOOOR");
                }}}).start();
}
//search
public void search() throws Exception {
         ResponseList<JSONObject> results = facebook.search("%23morocco");
         System.out.println(results);
         for (JSONObject result : results) {
             System.out.println(result);
         }

         results = facebook.search("orange", new Reading().until("yesterday"));
         System.out.println(results);
         for (JSONObject result : results) {
             System.out.println(result);
         }
     }

I replaced * with facebook api key I have a exception probleme , the error is : java.lang.IllegalStateException: No Token available. 我用facebook api键替换了*我有一个例外问题,错误是:java.lang.IllegalStateException:没有令牌可用。 ERROOOOOOR ERROOOOOOR

This is how you could use facebook4j without external configuration files. 这是你如何在没有外部配置文件的情况下使用facebook4j。 The code below provides a minimal example. 下面的代码提供了一个最小的例子。 Here is my simple demo: 这是我的简单演示:

import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.auth.AccessToken;

public class Facebook4JMinimalExample {

/**
 * A simple Facebook4J client.
 * 
 * 
 * @param args
 * @throws FacebookException 
 */
public static void main(String[] args) throws FacebookException {

// Generate facebook instance.
Facebook facebook = new FacebookFactory().getInstance();
// Use default values for oauth app id.
facebook.setOAuthAppId("", "");
// Get an access token from: 
// https://developers.facebook.com/tools/explorer
// Copy and paste it below.
String accessTokenString = "PASTE_YOUR_ACCESS_TOKEN_STRING_HERE";
AccessToken at = new AccessToken(accessTokenString);
// Set access token.
facebook.setOAuthAccessToken(at);

// We're done.
// Write some stuff to your wall.
facebook.postStatusMessage("Wow, it works...");
}
}

Note that it is important to FIRST make a call to "facebook.setOAuthAppId(..)" and THEN set the access token. 请注意,首先调用“facebook.setOAuthAppId(..)”然后设置访问令牌非常重要。 Otherwise you'll get an IllegalStateException saying "OAuth app id/secret combination not supplied". 否则,您将收到IllegalStateException,说“未提供OAuth应用程序ID /秘密组合”。

In this case, I've just used a default value for OAuthAppId. 在这种情况下,我刚刚使用了OAuthAppId的默认值。

You forgot to set the access token with fac.setOAuthAccessToken("*****") . 您忘记使用fac.setOAuthAccessToken("*****")设置访问令牌。 From the docs (emphasis mine): 文档 (强调我的):

All Graph API search queries require an access token passed in with the access_token=<token> parameter. 所有Graph API搜索查询都需要使用 access_token=<token>参数传入的访问令牌 The type of access token you need depends on the type of search you're executing. 您需要的访问令牌类型取决于您正在执行的搜索类型。

  • Searches across page and place objects requires an app access token. pageplace对象搜索需要应用程序访问令牌。
  • All other endpoints require a user access token . 所有其他端点都需要用户访问令牌

You can generate one for yourself here , but remember that these access tokens have an expiration time. 您可以在此处为自己生成一个,但请记住这些访问令牌具有到期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM