- Home /
Capture redirection Url
Hello,
I am trying to capture the access token of Facebook which is apparently provided in a redirection URL. So I call the following function in my application:
Application.OpenURL(String.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&state={2}", _facebookAppId, _redirectEndpoint, token));
_redirectEndpoint is my Server (www.someWebServer.com).
"When using a desktop app and logging in, Facebook redirects people to the redirect_uri mentioned above and places an access token along with some other metadata (such as token expiry time) in the URI fragment:"
is what it says on the Facebook guide
So I would like to create a script that captures the entire URI, so that I can retrieve in with my application. It should not be that hard but unfortunately I am not that familiar with server side scripting so it would be great tif somebody could help me out :)
Sorry about the necro. Did you end up finding a solution to this?
Answer by Bunny83 · Mar 16, 2016 at 05:14 PM
Application.OpenURL on most platforms opens that URL in a webbrowser so it has nothing to do with your application anymore. If called in a webbuild you effectively redirect the user to that new URL which in turn will replace the website where your webplayer / WebGL build run on.
You don't want to use Application.OpenURL. Such dynamic load operations are usually done with the WWW class. However in this case it can't be used since the WWW class automatically follows redirect responses (at least that's what the behaviour was in the past, haven't checked recently). That means you don't get the intermediate result which contains the information you need.
There are basically two ways:
At the redirect URL that you give facebook you should run some php script that simply echos back the URL parameters that it receives. When doing so you can use the WWW class to perform that task. The WWW class would automatically redirect to your given URL including the information passed by facebook. That means what the WWW class will return is what your PHP script returns. That way you can gather the information needed.
Use a socket implementation that doesn't do a transparent redirect. When developing for standalone, mobile or Webplayer this should be possible with the usual .Net classes from the
System.Net
namespace. However in a WebGL build you can't use those classes. There you would need to do the request in native JavaScript using an XMLHttpRequest or similar AJAX interface that the browser offers.
I just read that the XMLHttpRequest also follows redirect responses transparently. Unless facebook uses a different response code than 301 you probably can't use the second solution for WebGL. This is probably for security reasons. So i guess the first approach is the only one that will actually work in all cases. The point of the "redirectEndpoint" is to ensure the information gets passed to that point only and there the information should be handled.
Just read that facebook seems to pass the information in the URI fragment. That's a problem since the fragment usually isn't sent to the server. So the PHP script can't read that information since it only exists on the clientside. As you can read in the SO post i linked in my answer it seems that some browsers provide a "responseURL" field which contains the URL that was actually loaded. However this doesn't seem to apply to all browsers. One quite nasty solution could be to load the request URL into a hidden iFrame on your webpage where you could run a javascript which extracts the information from the iFrame URL.