Jak wygenerować token dostępu za pomocą odśwież token przez Google drive API?

Wykonałem kroki autoryzacji i uzyskałem token dostępu i odśwież token.

Co należy zrobić, aby wygenerować token dostępu za pomocą odświeżania tokena przechowywanego przez Google drive API?

Nie będę mógł używać żadnego sdk, ponieważ pracuję nad Force.com więc proszę zasugerować sposób wdrożenia go bezpośrednio przez API.

Author: Habeeb Perwad, 2012-05-17

9 answers

Jeśli chcesz zaimplementować to samodzielnie, przepływ OAuth 2.0 Dla aplikacji serwerowych jest udokumentowany pod adresem https://developers.google.com/accounts/docs/OAuth2WebServer , w szczególności powinieneś sprawdzić sekcję o używaniu tokena odświeżania:

Https://developers.google.com/accounts/docs/OAuth2WebServer#refresh

 31
Author: Claudio Cherubino,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-17 16:17:45

Jeśli używasz web api, powinieneś wykonać http POST wywołanie URL: https://www.googleapis.com/oauth2/v4/token z następującym ciałem żądania

client_id: <YOUR_CLIENT_ID>
client_secret: <YOUR_CLIENT_SECRET>
refresh_token: <REFRESH_TOKEN_FOR_THE_USER>
grant_type: refresh_token

Odśwież token nigdy nie wygasa, więc możesz go używać dowolną liczbę razy. Odpowiedź będzie taka jak JSON:

{
  "access_token": "your refreshed access token",
  "expires_in": 3599,
  "scope": "Set of scope which you have given",
  "token_type": "Bearer"
}
 33
Author: Ashutosh Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-30 23:20:08

To stare pytanie, ale wydaje mi się, że nie było całkowicie odpowiedzi, a ja też potrzebowałem tej informacji, więc opublikuję swoją odpowiedź.

Jeśli chcesz korzystać z biblioteki klienta Google Api, wystarczy mieć token dostępu, który zawiera token odświeżania, a następnie - nawet jeśli token dostępu wygaśnie po godzinie-biblioteka odświeży token dla ciebie automatycznie.

Aby uzyskać token dostępu z tokenem odświeżania, wystarczy poprosić o Typ dostępu offline (na przykład w PHP: $client->setAccessType("offline");) i otrzymasz go. Pamiętaj tylko, że token dostępu z tokenem odświeżania otrzymasz tylko podczas pierwszej autoryzacji, więc pamiętaj, aby zapisać ten token dostępu za pierwszym razem, a będziesz mógł z niego korzystać w dowolnym momencie.

Mam nadzieję, że to komuś pomoże: -)

 25
Author: amosmos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-26 16:35:02

Wszystko, co musisz zrobić, to żądanie post jak poniżej: -

POST https://www.googleapis.com/oauth2/v4/token
Content-Type: application/json

{
  "client_id": <client_id>,
  "client_secret": <client_secret>,
  "refresh_token": <refresh_token>,
  "grant_type": "refresh_token"
}
 7
Author: Raad Altaie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-01 07:37:04

Jeśli używasz Javy, wykonaj poniższy fragment kodu:

GoogleCredential refreshTokenCredential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY).setTransport(HTTP_TRANSPORT).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build().setRefreshToken(yourOldToken);
refreshTokenCredential.refreshToken(); //do not forget to call this
String newAccessToken = refreshTokenCredential.getAccessToken();
 3
Author: Divyesh Kanzariya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-17 10:32:47

Po prostu zamieszczam swoją odpowiedź na wypadek, gdyby komuś to pomogło, ponieważ spędziłem godzinę, aby to rozgryźć:)

Przede wszystkim dwa bardzo pomocne linki związane z google api i pobieranie danych z dowolnej usługi google:

Https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php

Https://developers.google.com/identity/protocols/OAuth2WebServer

Ponadto, przy użyciu następującej metody:

$client->setAccessToken($token)

The $token musi to być pełny obiekt zwracany przez google podczas składania żądania autoryzacji, a nie jedyny access_token, który dostajesz wewnątrz obiektu, więc jeśli otrzymasz obiekt powiedzmy:

{"access_token":"xyz","token_type":"Bearer","expires_in":3600,"refresh_token":"mno","created":1532363626}

Następnie należy podać:

$client->setAccessToken('{"access_token":"xyz","token_type":"Bearer","expires_in":3600,"refresh_token":"mno","created":1532363626}')

Nie

$client->setAccessToken('xyz')

I wtedy nawet jeśli Twoje access_token wygasło, google samo je odświeży używając refresh_token w obiekcie access_token.

 2
Author: Learner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-24 12:11:33

POST / OAuth2 / v4 / token

Host: www.googleapis.com

Headers

Content-length: 163

Content-type: application / x-www-form-urlencoded

RequestBody

Client_secret=************&grant_type=refresh_token&refresh_token=sasasdsa1312dsfsdf&client_id=************

 1
Author: Gaurav Bahl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-28 20:03:48

Za pomocą ASP.Net po telefonie, to mi się udało.

StringBuilder getNewToken = new StringBuilder();
getNewToken.Append("https://www.googleapis.com/oauth2/v4/token");                        
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(getNewToken.ToString());
                    var values = new Dictionary<string, string>
                    {
                        { "client_id", <Your Client Id> },
                        { "client_secret", <Your Client Secret> },
                        { "refresh_token", <Your Saved Refresh Token> },
                        { "grant_type", "refresh_token"}
                    };

                    var content = new FormUrlEncodedContent(values);
                    var response = await client.PostAsync(getNewToken.ToString(), content);
 1
Author: AtLeastTheresToast,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-15 11:58:13

Korzystanie z post call, zadziałało dla mnie.

RestClient restClient = new RestClient();
RestRequest request = new RestRequest();

request.AddQueryParameter("client_id", "value");
request.AddQueryParameter("client_secret", "value");
request.AddQueryParameter("grant_type", "refresh_token");
request.AddQueryParameter("refresh_token", "value");

restClient.BaseUrl = new System.Uri("https://oauth2.googleapis.com/token");
restClient.Post(request);

Https://youtu.be/aHs3edo0-mU

 0
Author: purshotam sah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-02-01 17:59:59