Server side google api authnetication using refresh token

Server side google api authnetication using refresh token



Getting your Refresh Token

Go to https://developers.google.com/oauthplayground.

Make sure you added this URL to your Authorized redirect URIs in the previous step. In the top right corner, click the settings icon, check "Use your own OAuth credentials" and paste your Client ID and Client Secret.

go to https://client-secret.web.app/ 

Use your own OAuth credentials

In step 1 on the left, scroll to "Drive API v3", expand it and check the first drive scope.

Check Scopes

Click "Authorize APIs" and allow access to your account when prompted. There will be a few warning prompts, just proceed.

When you get to step 2, check "Auto-refresh the token before it expires" and click "Exchange authorization code for tokens".

Exchange authorization code for tokens

When you get to step 3, click on step 2 again and you should see your refresh token.

Refresh Token

def create_service(CLIENT_SECRET_FILE, REFRESH_TOKEN, SCOPES, API_SERVICE_NAME,API_VERSION):
    f = open(CLIENT_SECRET_FILE, "r")
    data = json.load(f)
    CLIENT_ID, CLIENT_SECRET = data['web']['client_id'], data['web']['client_secret']
    print(CLIENT_ID,CLIENT_SECRET, CLIENT_SECRET_FILE)
    creds = Credentials.from_authorized_user_info(info={'refresh_token': REFRESH_TOKEN,'client_id': CLIENT_ID,'client_secret': CLIENT_SECRET}, scopes=SCOPES)
    return build(API_SERVICE_NAME, API_VERSION, credentials=creds)

Post a Comment

Previous Post Next Post