Google API - How to redirect URL to my localhost

别等时光非礼了梦想. 提交于 2020-02-13 03:02:07

问题


I am working on integrating google login in my Phonegap app using Google OAuth. What happens is that while creating a client ID for my app, I have to choose "Installed Application" and then the application type as "Others" since I am creating my app using Phonegap.

This gives me two redirect URIs such as "urn:ietf:wg:oauth:2.0:oob" and "http://localhost". I am not opting to go with "urn:ietf:wg:oauth:2.0:oob" as it requires the user to copy the code and put it back to the app. The other option that I have is redirecting to localhost.

Here the problem is that how do I redirect the URL to localhost when I am on iPad? I have tried to use different redirect URIs but Google doesn't let me use them. It sticks only with redirecting to localhost (or adding some port numbers) which I have no other go.

Does anyone have any workaround on this? Are there any methods to accomplish this task? I am stuck. Please help.


回答1:


You can use Rewrite Rules too.

For example, I need this callback for Google : http://local.dev/users/login-google

So I have set this URL in Google Console : http://localhost/JUMP/local.dev/users/login-google

And in apache, a simple redirect :

RewriteEngine  on
RewriteRule    ^JUMP/(.+)$   http://$1 [R,L]



回答2:


I have had the same issue recently. I also needed to figure it out for the Twitter API (answer found here).

For google, I found 2 ways to fix the issue.

  1. Google will accept a localhost callback url. For example, if you are on a xampp server, and your redirect is saved under C:\xampp\htdocs\nozzle\callbacks\google.php then your redirect to give in the request (and in the Google API Console) would be: http://localhost/nozzle/callbacks/google.php Promblem Solved.
  2. If you are developing on a vhosted site: Redirect to a live site (or to somewhere in your localhost folder structure) to communicate with the google api and then serialize that data and send it to your url on your local virtual host.

Option 2 requires some more explaining. Here's the breakdown:

  • I am developing locally on a virtual host. My data for my app is not in the localhost folder (http://myapp.dev). So I needed another solution that would allow me to still develop locally on a vhost, but also make requests to the Google API.
  • The Setup: the first call is made from http://myapp.dev/ but for the redirect, I use my live server address. Upon authorization from the user, Google redirects back to my live site where I make an additional call to the api. This info comes back (again to my live site) and I then serialize that info and redirect to http://myapp.dev/callback as a request by adding ?data= and put the serialized data at the end of that call. It then gets sent to my vhost site, and I retreive the data with $_REQUEST['data'], unserialize it, and I'm good to go.
  • This may seem like a lot of work, but for me and my team it is worth it to continue to work locally until we are ready to go live.

Also, I imagine you could do the same process through a localhost address rather than a live one, but I have not tested that. But, if you do it with a live site, your whole team will be able to make the same requests from their local vhosts, as long as they are also vhosted at http://myapp.dev

Foot Note: I am using the socialmedia-oauth-login tool for accessing the Google API.



来源:https://stackoverflow.com/questions/14053381/google-api-how-to-redirect-url-to-my-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!