B
B
Bogdan Pasechnik2016-01-26 13:18:49
Google
Bogdan Pasechnik, 2016-01-26 13:18:49

What is my mistake when authorizing google via oauth2?

I need to set up integration with google calendar. To do this, I found out you need to get a refresh_token from google. Through it, I can get an access token. And then work through the api.
To get a refresh_token, I form the following link [link 1]

https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri={url}&client_id={client_id}&scope=https://www.googleapis.com/auth/calendar&access_type=offline&approval_prompt=auto

When testing on localhost, clicking on the specified link opens the clip2net.com/s/3tqRiI8 window (hereinafter window 1). After confirmation, I get to the address {url} with the transferred data. And then everything works fine.
The problem arose when I uploaded the code to the server. I added a new redirect_uri no longer on localhost. And when you click on the authorization link [link 1] [window 1] with confirmation, it does not pop up, but immediately redirects to {url}. And I can't get refresh_token.
Tell me what I'm doing wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Melnichenko, 2016-01-26
@alex87melnichenko

If it immediately redirects to {url}, then it means that it sees that you have already given access, and therefore does not request it. It will redirect to {url} and also pass $_GET['code'] as a parameter. Here you need to get a token through a post request to ' https://accounts.google.com/o/oauth2/token ' with parameters
$params = array(
'client_id' => 'application id',
'client_secret' => 'application secret',
'redirect_uri' => 'redirect url',
'grant_type' => 'authorization_code',
'code' => $_GET['code']
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question