Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with facebook authentication when user switches accounts
    primarykey
    data
    text
    <p>I'm having an issue with authenticating facebook users via oauth 2.0 in my java web app. The problem seems to happen when a user that's using my app logs out of their facebook account and logs back into another facebook account. Then when I attempt to authenticate the new facebook account, facebook responds with a 400 error. If the user then switches back the first account, then facebook will authenticate them just fine. </p> <p>I think the problem is that when I request a code from facebook, it returns the same code for both users, but the second user can't get an access token with that code because the code was made for the first user. </p> <p>Does anyone know how I can fix this? Here is a stripped down version of my code. Note, I have an interceptor that automatically redirects users to FacebookDirectorAction if the page requires them to be signed in. So FacebookDirectorAction handles getting a code, and access token, and finally redirecting the user back to the original page they requested.</p> <pre><code>@UrlBinding("/facebookdirector/{destination}/{destinationParameter}") public class FacebookDirectorAction extends BaseActionBean { private static final String BASE_REDIRECT_URI = "http://apps.facebook.com/lawlessdev/facebookdirector"; private static final String APP_ID = "My app id"; private static final String APP_SECRET = "My secret"; public static final String AUTH_VIEW = VIEW_PATH + "auth.jsp"; private String destination; private String destinationParameter; private String code; private String access_token; private String authUrl; @DefaultHandler public Resolution direct() throws IOException, FacebookException, ServletException { if (access_token == null &amp;&amp; code == null) { return authorize(); } else if (access_token == null) { retrieveAccessToken(); } // If access_token is still null then we may mave a bad code. Redirect to an insecure page. if (access_token == null) { return new RedirectResolution(CategoriesAction.class); } RedirectResolution redirectResolution = new RedirectResolution("/" + destination + (destinationParameter != null ? "/" + destinationParameter : "")); redirectResolution.addParameter("access_token", access_token); return redirectResolution; } public Resolution authorize() throws IOException, ServletException { authUrl = "https://graph.facebook.com/oauth/authorize?client_id=" + APP_ID + "&amp;redirect_uri=" + getRedirectUri(); return new ForwardResolution(AUTH_VIEW); } public void retrieveAccessToken() throws UnsupportedEncodingException { try { URL accessTokenURL = new URL("https://graph.facebook.com/oauth/access_token?client_id=" + APP_ID + "&amp;client_secret=" + APP_SECRET + "&amp;code=" + code + "&amp;redirect_uri=" + getRedirectUri()); URLConnection accessTokenURLConnection = accessTokenURL.openConnection(); accessTokenURLConnection.connect(); BufferedReader in = new BufferedReader( new InputStreamReader( accessTokenURLConnection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { ParameterParser parameterParser = new ParameterParser(); List&lt;NameValuePair&gt; nameValuePairs = parameterParser.parse(inputLine, '&amp;'); for (NameValuePair nameValuePair : nameValuePairs) { if (nameValuePair.getName().equals("access_token")) { access_token = nameValuePair.getValue(); } } } in.close(); } catch (MalformedURLException e) { // new URL() failed } catch (IOException e) { // openConnection() failed } } private String getRedirectUri() throws UnsupportedEncodingException { return BASE_REDIRECT_URI + "/" + destination + (destinationParameter != null ? "/" + destinationParameter : ""); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload