Sankar Samanta
Sankar Samanta

Reputation: 11

How to update access token using refresh token in you tube api?

I have tried this to update my access token

import urllib
endpoint='https://accounts.google.com/o/oauth2/token'
data={'client_id':'25********15-6*********************7f.apps.googleusercontent.com','client_secret':'4********Pj-K*****x4aM','refresh_token':'1/tP************************O_XclU','grant_type':'refresh_token'}
encodedData=urllib.urlencode(data)
from httplib2 import Http
h = Http()
resp, content = h.request(endpoint, "POST", encodedData)

But got the error message

'{\n  "error" : "invalid_request",\n  "error_description" : "Required parameter is missing: grant_type"\n}'

Upvotes: 0

Views: 727

Answers (2)

frank
frank

Reputation: 656

old-refresh_token is the refresh token you have

CLIENT_ID,CLIENT_SECRET are the credentials you can find those in google developers console

http = httplib2.Http()
TOKEN_URL = "https://accounts.google.com/o/oauth2/token"

headers = {'Content-Type': 'application/x-www-form-urlencoded'}
parameters = urllib.urlencode({'refresh_token': old-refresh_token, 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'grant_type': 'refresh_token'})
resp, response_token = http.request(TOKEN_URL, method='POST', body=parameters, headers=headers)
token_data = json.loads(response_token)
access_token = token_data['access_token']

the variable access_token now holds your access token

try it out

Upvotes: 0

gunererd
gunererd

Reputation: 661

You should specify the headers in your request like this:

resp, content = h.request(uri=endpoint,
                          method="POST", 
                          body=encodedData, 
                          headers={'Content-type': 'application/x-www-form-urlencoded'})

Upvotes: 1

Related Questions