How can i get my accessToken, if i use the link:

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

I want to get the token in Java.

//EDIT:

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);

//EDIT: i have save the token.txt file. the code is like thisenter image description here:

有帮助吗?

解决方案

Use following code. It will return you a map of all the query parameters

        URL newURL = new URL(GraphURL1);
        HttpsURLConnection https = (HttpsURLConnection) newURL.openConnection();
        https.setRequestMethod("HEAD");
        https.setUseCaches(false);
        String query = newURL.getQuery();

Use following code. It will return you a map of all the query parameters

Map<String, String> queryMap = getQueryMap(query );

Method to get query Map

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;  
}

following method will write your token into a file, you just need to pass the token

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();
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top