Dynamic Google API redirect URL

André Figueira picture André Figueira · May 13, 2012 · Viewed 9.4k times · Source

I am coding a google contacts import code for a social network this import happens on the user page which the url will vary from user to user e.g. profile/user1, profile/user2

However with google I seem to be able to set only one redirect url and can't seem to find any mention of google allowing wildcards to match the domain instead of the particular url.

Is there a way of doing this or will I have to just leave it set to one url?

Thanks in advance.

Answer

Fraz Ahmed picture Fraz Ahmed · Oct 15, 2013

I have the PHP code to achieve it. It is wrong to say that it cannot be done. I used this technique for Analytics, Adwords, Google+ and YouTube. It worked with all mentioned services.

Trick is to use "state" parameter to be used as dynamic URL. I hope it will help everyone.

// Auth URL
// $campaign_id will be different for everyone
$dynamic_redirect = 'http://' . $_SERVER['HTTP_HOST'] . "/analytics/$campaign_id";
$client_id = 'XXXXXXXX';
$redirect_uri = 'API_REDIRECT_URI'; // Fixed URL, it will not be changed

$auth_url = "https://accounts.google.com/AccountChooser?service=lso&continue=";
$auth_url .= urlencode("https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.googleapis.com/auth/analytics.readonly&access_type=offline&redirect_uri=$redirect_uri&approval_prompt=force&state=$dynamic_redirect&client_id=$client_id");


/*************************************/
API_REDIRECT_URI PAGE
/*************************************/
$redirect_url = $_GET['state'];