<commit_before>from oauth2client.client import OAuth2WebServerFlow
from os import environ

if False:

    CLIENT_ID = environ['GDOCS_CLIENT_ID']
    CLIENT_SECRET = environ['GDOCS_CLIENT_SECRET']
    SCOPES = ['https://spreadsheets.google.com/feeds/']

    flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, " ".join(SCOPES))
    flow_info = flow.step1_get_device_and_user_codes()
    print "Enter the following code at %s: %s" % (flow_info.verification_url, flow_info.user_code)

    raw_input()

    credentials = flow.step2_exchange(device_flow_info=flow_info)
    print "Access token:", credentials.access_token
    print "Refresh token:", credentials.refresh_token

else:
    
    class FakeCred:
        access_token = '<PASSWORD>'
        #refresh_token = '<PASSWORD>'
    
    credentials = FakeCred()

import gspread
gc = gspread.authorize(credentials)

doc = gc.open_by_url('https://docs.google.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

print doc.worksheets()<commit_msg>Successfully copied a template spreadsheet<commit_after>from oauth2client.client import OAuth2WebServerFlow
from os import environ

class FakeCred:
    access_token = 'ya29.<KEY> <PASSWORD>ELn<PASSWORD>'
    #refresh_token = '<PASSWORD>'

if True:

    CLIENT_ID = environ['GDOCS_CLIENT_ID']
    CLIENT_SECRET = environ['GDOCS_CLIENT_SECRET']
    SCOPES = ['https://spreadsheets.google.com/feeds/',

              # http://stackoverflow.com/questions/24293523/im-trying-to-access-google-drive-through-the-cli-but-keep-getting-not-authori
              'https://docs.google.com/feeds',
              
              #'https://www.googleapis.com/auth/drive',
              #'https://www.googleapis.com/auth/drive.file',
              #'https://www.googleapis.com/auth/plus.login',
              ]

    try:
        flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, SCOPES)
        flow_info = flow.step1_get_device_and_user_codes()
        print "Enter the following code at %s: %s" % (flow_info.verification_url, flow_info.user_code)
    except Exception as e:
        print e.args, e.message
        raise

    raw_input()

    credentials = flow.step2_exchange(device_flow_info=flow_info)
    print "Access token:", credentials.access_token
    print "Refresh token:", credentials.refresh_token

else:
    
    credentials = FakeCred()

import gspread, json

try:
    gc = gspread.authorize(credentials)
    resp = gc.session.post('https://www.googleapis.com/drive/v2/files/12jUfaRBd-CU1_6BGeLFG1_qoi7Fw_vRC_SXv36eDzM0/copy', '{}', headers={'Content-Type': 'application/json'})
except Exception as e:
    print e
    print e.response.getheaders()
    print e.response.read()
    raise

info = json.load(resp)
print '{id} - {title}'.format(**info)

try:
    gc = gspread.authorize(credentials)
    url = 'https://www.googleapis.com/drive/v2/files/{id}'.format(**info)
    gc.session.request('PATCH', url, '{"title": "Good TIMES"}', headers={'Content-Type': 'application/json'})
except Exception as e:
    print e
    print e.response.getheaders()
    print e.response.read()
    raise

exit()

doc = gc.open_by_url('https://docs.google.com/a/codeforamerica.org/spreadsheets/d/12jUfaRBd-CU1_6BGeLFG1_qoi7Fw_vRC_SXv36eDzM0/edit#gid=0')

print doc.worksheets()
{'fraction_non_alphanumeric': 0.12704286154794944, 'fraction_numerical': 0.012642614862781376, 'mean_word_length': 3.6676258992805755, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 10, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6496900', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1122, 'n_words': 184}