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