Esempio n. 1
0
def main(argv):
    parser = create_parser()
    opts = parser.parse_args(argv[1:])

    flow = oauth2client.client.OAuth2WebServerFlow(
        client_id=opts.client_id,
        client_secret=opts.client_secret,
        scope=hyou.SCOPES)
    url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob')

    print()
    print('Please visit this URL to get the authorization code:')
    print(url)
    print()

    code = py3.input('Code: ').strip()

    credentials = flow.step2_exchange(code)

    with py3.open(opts.output_json_path, 'wb') as f:
        os.fchmod(f.fileno(), 0o600)
        f.write(
            py3.native_str_to_bytes(credentials.to_json(), encoding='utf-8'))

    print()
    print('Credentials successfully saved to %s' % opts.output_json_path)
    print()
    print('WARNING: Keep it in a safe location! With the credentials,')
    print('         all your Google Drive documents can be accessed.')
Esempio n. 2
0
 def __init__(self, json_name):
     if not json_name:
         self._real_http = None
     else:
         json_path = os.path.join(os.path.dirname(__file__), 'creds',
                                  json_name)
         with py3.open(json_path, 'r') as f:
             credentials = hyou.util.parse_credentials(f.read())
         self._real_http = credentials.authorize(httplib2.Http())
     self._records = _load_records()
Esempio n. 3
0
def _load_records():
    records = {}
    for filename in sorted(os.listdir(RECORDS_DIR)):
        record_path = os.path.join(RECORDS_DIR, filename)
        with py3.open(record_path, 'r', encoding='utf-8') as f:
            record = json.load(f)
            record['_path'] = record_path
            body_bytes = (record['request'].encode('utf-8')
                          if record['request'] else None)
            sig = _build_signature(method=record['method'],
                                   uri=record['uri'],
                                   body=body_bytes)
            assert sig not in records, 'dup response: %s' % filename
            records[sig] = record
    return records
Esempio n. 4
0
 def test_login_invalid(self):
     json_path = os.path.join(os.path.dirname(__file__), 'creds',
                              'example-invalid.json')
     with py3.open(json_path) as f:
         self.assertRaises(ValueError, hyou.util.parse_credentials,
                           f.read())
Esempio n. 5
0
 def test_login_bot(self):
     json_path = os.path.join(os.path.dirname(__file__), 'creds',
                              'example-bot.json')
     with py3.open(json_path) as f:
         hyou.util.parse_credentials(f.read())