8

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
André Figueira
  • 6,048
  • 14
  • 48
  • 62
  • In case anyone came here for sign in and is using cognito, when calling `federatedSignIn` you can pass a property called `customState` and then you can have some code listening on `Hub.listen` for the event `customOAuthState` which will give you access to the custom state once the user is signed in – maxime1992 Jan 12 '21 at 12:46

5 Answers5

18

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'];
Fraz Ahmed
  • 288
  • 2
  • 5
6

I have found that this is not possible, so if anyone is looking for this, there isn't a way. I ended up fixing my issue by just letting google redirect to a fixed url so not a dynamic one.

André Figueira
  • 6,048
  • 14
  • 48
  • 62
  • 2
    You pass the dynamic part as the "state" parameter as @fraz-ahmed mentioned. The redirect URL will include this state value as a query parameter. You can encoder whatever you need into the state. – philidem Apr 17 '15 at 15:28
1

You can do one simple thing. Put "user page url" in session when creating Auth URL. On your Callback page get "user page url" from session and simple redirect user to that page.

I was able to achieve above successfully using PHP.

Fraz Ahmed
  • 288
  • 2
  • 5
  • would you share some example code on how you send the user page URL, and receive it in the response please. Maybe in pastebin, thank you – Anagio Sep 21 '13 at 23:58
  • @anagio check my new answer for the code: [link](http://stackoverflow.com/questions/10569795/dynamic-google-api-redirect-url/19382579#19382579) – Fraz Ahmed Oct 15 '13 at 13:33
  • By the way in my new answer we even don't need any SESSION. It is all dynamic. – Fraz Ahmed Oct 19 '13 at 06:49
0

It is possible, as I know of an app that does this. I found this post on how to do it - haven't tried it yet, but it's worth a shot: http://www.ioncannon.net/programming/1443/google-oauth-for-installed-apps-php-example/

Shane N
  • 1,742
  • 2
  • 17
  • 24
  • 1
    Note that link-only answers are discouraged (links tend to get stale over time). Please consider editing your answer and adding a synopsis here. – kleopatra Jun 19 '13 at 17:13
-2

We can specify the multiple Redirect URIs in the Google API Settings, one per line

Google API Console -> Select your API -> API Access -> Edit Setting -> Under Authorize Redirection URI

enter..

http://one.example.com/contactimporter.php

http://two.example.com/contactimporter.php

Sathish Kumar
  • 369
  • 1
  • 3
  • 10