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