Facebook graph api 我如何获取访问令牌并使用它?

Facebook graph api 我如何获取访问令牌并使用它?,facebook-graph-api,facebook,facebook-java-api,facebook-java-sdk,Facebook Graph Api,Facebook,Facebook Java Api,Facebook Java Sdk,在我的web应用程序中,我希望用户使用他们的facebook帐户登录。我是通过重定向url来实现的 response.sendRedirect("http://www.facebook.com/dialog/oauth/?scope=email,user_about_me&dispplay=popup&client_id={app_id}&redirect_uri=http://example.com/index.jsp&response_type=token"

在我的web应用程序中,我希望用户使用他们的facebook帐户登录。我是通过重定向url来实现的

 response.sendRedirect("http://www.facebook.com/dialog/oauth/?scope=email,user_about_me&dispplay=popup&client_id={app_id}&redirect_uri=http://example.com/index.jsp&response_type=token");
在index.jsp上,我获得了url中的访问令牌。但在请求中没有访问令牌。如何在此处获取访问令牌,然后获取用户电子邮件。index.jsp上的url如下所示:

  http://example.com/index.jsp#access_token={access_token}

提前感谢。

显示您应该获取令牌作为请求参数。只是不要使用
response\u type=token
我找到了一个教程,它解决了我的问题。以下是该季刊的链接:


事实上,我刚刚在我的JSP项目中实现了这一点!好的,这是你需要知道的。您尝试使用的方法称为“服务器端”方法(还有一种客户端方法,它可以共享许多相同的代码。您将使用两个URL(我已将这一切打包到一个名为FacebookConfig的助手类中,它从facebook.properties文件中读取所需内容:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import lombok.Cleanup;
import st.fan.model.users.Listener;

public class FacebookConfig {
  public static String auth_uri;
  public static String id;
  public static String key;

  static{
    Properties properties = new Properties();
    try {
      @Cleanup InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("facebook.properties");
      properties.load(in);
      auth_uri = (String)properties.get("auth_uri");
      id = (String)properties.get("id");
      key = (String)properties.get("key");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static String getOAuthDialogUrl() {
    return "https://www.facebook.com/dialog/oauth?client_id="+id+"&redirect_uri="+auth_uri+"&scope=email";
  }

  public static String getOAuthUrl(String code) {
    return "https://graph.facebook.com/oauth/access_token?client_id="+id+"&redirect_uri="+auth_uri+"&client_secret="+key+"&code="+code;
  }

  public static String getGraphUrl(String token) {
    return "https://graph.facebook.com/me?access_token="+token;
  }

  public static String getProfilePictureUrl(Listener profile) {
    return "https://graph.facebook.com/"+profile.getFacebookId()+"/picture";
  }

  public static String getProfilePictureUrl(Listener profile, String size) {
    return "https://graph.facebook.com/"+profile.getFacebookId()+"/picture?type="+size;
  }
}
现在,用户被重定向到getOAuthDialogUrl(),其中包括一个名为的servlet的URL,该servlet具有这个doGet()方法

(除了使用Hibernate、Project Lombok和一个名为org.JSON的简单JSON库之外),我还使用了一个名为fetch()的简单帮助函数,该函数获取URL并以字符串形式返回内容,该字符串包含以下代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class NetUtils {
  public static String fetch(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    String line;
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    while((line = reader.readLine()) != null) {
      builder.append(line);
    }
    return builder.toString();
  }
}

这应该可以让您起步:)

只需打印$\u REQUEST['signed\u REQUEST']数组,其中就有oauth。这是您的活动访问令牌,您可以在任何地方使用它


我自己花了两天的时间在它上面,最后得到了它。我使用了response\u type=code。我得到了作为请求参数的代码。现在如何获得令牌?我找到了这个教程,但我有点困惑。我得到了代码,但我如何才能获得访问令牌。如果我尝试将它重定向到其他servlet,那么它会给出错误。这意味着请求获取代码和访问令牌应该是一样的。这怎么可能呢?这就像对同一个页面的无限调用。不是吗?从哪里可以获得st.fan.model.users.Listener jar?我在实现了我发布的这个问题的答案之后,得到了这个答案。Listener类基本上就是我所说的Profile类。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class NetUtils {
  public static String fetch(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    String line;
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    while((line = reader.readLine()) != null) {
      builder.append(line);
    }
    return builder.toString();
  }
}