本文介绍了RestFB:使用Facebook应用程序获取用户访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的:

  static AccessToken accessToken = new DefaultFacebookClient()。获取ExtendedAccessToken(<我的应用程序ID> ;,<我的应用秘密>); 
static FacebookClient client = new DefaultFacebookClient();
public static void main(String args []){
System.out.print(Enter Your Status:);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String status = null;

try {
userName = br.readLine();
System.out.println(..........);
} catch(IOException ioe){
System.out.println(!);
System.exit(1);
}

FacebookType publishMessageResponse =
client.publish(me / feed,FacebookType.class,
Parameter.with(message,status));因此,第一行获取令牌并将其存储为AccessToken类型,但对我来说有什么好处呢?下一行我需要提供访问令牌作为一个字符串,我不能转换它。任何帮助?

解决方案

除了Jack所说的AccessToken.getAccessToken()返回accessToken的字符串值之外,您可以避免实例化 DefaultFacebookClient 通过扩展 DefaultFacebookClient 两次,如下所示:

  import com.restfb.DefaultFacebookClient; 

public class LoggedInFacebookClient extends DefaultFacebookClient {

public LoggedInFacebookClient(String appId,String appSecret){
AccessToken accessToken = this.obtainAppAccessToken(appId,appSecret);
this.accessToken = accessToken.getAccessToken();
}

}


This is what i have:

static AccessToken accessToken = new DefaultFacebookClient().obtainExtendedAccessToken("<my app id>", "<my app secret>");
static FacebookClient client = new DefaultFacebookClient();
public static void main(String args[]) {
    System.out.print("Enter Your Status: ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String status= null;

      try {
         userName = br.readLine();
         System.out.println("..........");
      } catch (IOException ioe) {
         System.out.println("!");
         System.exit(1);
      }

    FacebookType publishMessageResponse =
                client.publish("me/feed", FacebookType.class,
                Parameter.with("message", status));

So first line gets the token and stores it as type AccessToken but what good does that do to me because next line i need to provide the access token as a string and i can't convert it. Any Help?

解决方案

In addition to what Jack said about AccessToken.getAccessToken() returning the string value of accessToken, you can avoid instantiating DefaultFacebookClient twice by extending DefaultFacebookClient like this:

import com.restfb.DefaultFacebookClient;

public class LoggedInFacebookClient extends DefaultFacebookClient {

    public LoggedInFacebookClient(String appId, String appSecret) {
        AccessToken accessToken = this.obtainAppAccessToken(appId, appSecret);
        this.accessToken = accessToken.getAccessToken();
    }

}

这篇关于RestFB:使用Facebook应用程序获取用户访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 14:53