• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2
3# See:  https://developers.google.com/accounts/docs/OAuth2ForDevices
4
5from googleapiclient.discovery import build
6import httplib2
7from six.moves import input
8
9from oauth2client.client import OAuth2WebServerFlow
10
11CLIENT_ID = "some+client+id"
12CLIENT_SECRET = "some+client+secret"
13SCOPES = ("https://www.googleapis.com/auth/youtube",)
14
15flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, " ".join(SCOPES))
16
17# Step 1: get user code and verification URL
18# https://developers.google.com/accounts/docs/OAuth2ForDevices#obtainingacode
19flow_info = flow.step1_get_device_and_user_codes()
20print("Enter the following code at {0}: {1}".format(flow_info.verification_url,
21                                                    flow_info.user_code))
22print("Then press Enter.")
23input()
24
25# Step 2: get credentials
26# https://developers.google.com/accounts/docs/OAuth2ForDevices#obtainingatoken
27credentials = flow.step2_exchange(device_flow_info=flow_info)
28print("Access token:  {0}".format(credentials.access_token))
29print("Refresh token: {0}".format(credentials.refresh_token))
30
31# Get YouTube service
32# https://developers.google.com/accounts/docs/OAuth2ForDevices#callinganapi
33youtube = build("youtube", "v3", http=credentials.authorize(httplib2.Http()))
34