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