sea*_*rit 3 java authentication facebook facebook-login facebook-access-token
我需要通过 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(...
Run Code Online (Sandbox Code Playgroud)
在这里获取令牌。这是正确的?
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){
}
Run Code Online (Sandbox Code Playgroud)
我认为没有办法以编程方式获取用户访问令牌。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);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,由于此请求使用您的应用程序密钥,因此绝不能在客户端代码或可反编译的应用程序二进制文件中进行。重要的是,您的应用程序秘密永远不会与任何人共享。因此,此 API 调用只能使用服务器端代码进行。
阅读此处:https ://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens
顺便说一句,我们Stormpath支持非常简单的社交登录集成(Google、Github、Facebook、LinkedIn)
| 归档时间: |
|
| 查看次数: |
8234 次 |
| 最近记录: |