如果我使用链接,我如何获得accessToken:
https://www.facebook.com/login.php?skip_api_login=1&api_key=MY_APP_TOKEN&signed_next=1&next=https://www.facebook.com/dialog/oauth?redirect_uri=http%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html&scope=read_stream%252Coffline_access&type=user_agent&client_id=389735501155841&ret=login&cancel_uri=http://www.facebook.com/connect/login_success.html?error=access_denied&error_code=200&error_description=Permissions%2berror&error_reason=user_denied#_=_&display=page
我想用Java获取令牌。
//编辑:
String GraphURL1 = "https://www.facebook.com/dialog/oauth?client_id=APPTOKEN&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";
URL newURL = new URL(GraphURL1);
HttpsURLConnection https = (HttpsURLConnection)newURL.openConnection();
https.setRequestMethod("HEAD");
https.setUseCaches(false);
//编辑:我已保存token.txt文件。代码就像这个:
答案 0 :(得分:1)
使用以下代码。它将返回所有查询参数的地图
URL newURL = new URL(GraphURL1);
HttpsURLConnection https = (HttpsURLConnection) newURL.openConnection();
https.setRequestMethod("HEAD");
https.setUseCaches(false);
String query = newURL.getQuery();
使用以下代码。它将返回所有查询参数的地图
Map<String, String> queryMap = getQueryMap(query );
获取查询地图的方法
public static Map<String, String> getQueryMap(String query )
{
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params)
{
String name = param.split("=")[0];
String value = param.split("=")[1];
map.put(name, value);
}
return map;
}
以下方法会将您的令牌写入文件,您只需要传递令牌
public void writeTokenIntoFile(String token)
{
try{
File file =new File("c://token.txt");
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(token);
bufferWritter.close();
System.out.println("Done");
}catch(IOException e){
e.printStackTrace();
}
}