3

我需要使用 Java 登录来获取我的 user_accessToken。我必须使用 FacebookOAuthResult 吗?如果是,如何?我编写了一个用于登录 Facebook 的 Applet。它有效,但我无法获得令牌。

...
String GraphURL = "https://graph.facebook.com/me/friends?&access_token=" + token;
URL newURL = URL(GraphURL);
HttpsURLConnection https = (HttpsURLConnection)newURL.openConnection();
https.setRequestMethod("HEAD");
https.setUseCache(false);
...

//open a connection window like:
if(Desktop.isDesktopSupported())
{
Desktop.getDesktop().browse(new URI(GraphURL));
}
else if(...

在这里获取令牌。这是正确的?

String getTokenUrl = "https://www.facebook.com/dialog/oauth?client_id=MY_APP_ID&redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&response_type=token&display=popup&scope=user_about_me%2Cread_stream%2C%20share_item";

Desktop desktop = Desktop.getDesktop();
desktop.browse(new URL(getTokenUrl).toURI());

URL tokenURL = new URL(getTokenUrl);
URLConnection connect = tokenURL.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));

String inputLine;
StringBuffer bufferr = new StringBuffer();
while ((inputLine = in.readLine()) != null)
bufferr.append(inputLine + "\n");
in.close();

token = bufferr.toString();

HttpURLConnection myWebClient = (HttpURLConnection)url.openConnection();
myWebClient.setRequestMethod("HEAD");
myWebClient.setUseCaches(false);
//webClient..
try
{
String gAntwort = myWebClient.getResponseMessage(); 

if (myWebClient.getResponseCode() != HttpURLConnection.HTTP_OK)
{
//Not OK
//JOptionPane.showMessageDialog(null, conn.getResponseMessage(), "URL Response", JOptionPane.INFORMATION_MESSAGE);
}
init();
} catch (Exception d){

}
4

2 回答 2

5

我认为没有办法以编程方式获取用户访问令牌。Facebook 需要用户的同意,以前登录的用户需要按“确定”按钮才能获得新的access_token.

application access token但是,如果这是您想要的,有一种方法可以得到。

 public static void main(String[] args) {
    try {

        String myAppId = "XXX";
        String myAppSecret = "XXX";
        String redirectURI = "http://localhost:8080/";

        String uri = "https://graph.facebook.com/oauth/access_token?client_id=" +
                myAppId +
                "&client_secret=" + myAppSecret +
                "&grant_type=client_credentials" +
                "&redirect_uri=" + redirectURI +
                "&scope=user_about_me";

        URL newURL = new URL(uri);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = newURL.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        String response = new String(baos.toByteArray());
        is.close();
        baos.close();

        String TOKEN_INDEX = "accesss_token=";
        String token = response.substring(TOKEN_INDEX.length()-1);

        //API Call using the application access token
        String graph = "https://graph.facebook.com/v2.2/" + myAppId +
                "?access_token=" + token;

        URL graphURL = new URL(graph);
        HttpURLConnection myWebClient = (HttpURLConnection) graphURL.openConnection();

        String responseMessage = myWebClient.getResponseMessage();

        if (myWebClient.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.out.println(myWebClient.getResponseCode() + " " + responseMessage);
        } else {
            System.out.println(responseMessage);
        }
        myWebClient.disconnect();

    } catch (Exception e) {
        System.err.println(e);
    }
}

请注意,由于此请求使用您的应用程序密码,因此绝不能在客户端代码或可反编译的应用程序二进制文件中进行。重要的是,您的应用程序机密永远不会与任何人共享。因此,此 API 调用只能使用服务器端代码进行。

在这里阅读:https ://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

顺便说一句,我们Stormpath支持非常简单的社交登录集成(Google、Github、Facebook、LinkedIn)

于 2015-02-03T16:33:25.683 回答
1

这应该有助于保存访问令牌http://www.youtube.com/watch?v=_JjWqwWWcVE

于 2013-10-29T08:36:22.870 回答