Help us understand the problem. What is going on with this article?

Google BigQuery (オライリー本)で出てくるauth.py

More than 3 years have passed since last update.

はじめに

オライリー本に出てくるauth.pyが古くて実行できなかったので、最新版に書き直してみた。

2017年4月8日(土)動作確認済み

auth.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import httplib2
import json
from apiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials

BIGQUERY_SCOPE = 'https://www.googleapis.com/auth/bigquery'
# 以下の値は自分のサービスアカウント用の秘密鍵のファイルへフルパスにすること
KEY_FILE = '<自分のサービスアカウント用の秘密鍵名>.json'

def get_oauth_creds():

    # ユーザークレデンシャルの生成。
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        KEY_FILE, 
        BIGQUERY_SCOPE
    )
    # 最新のクレデンシャルに更新。
    credentials.refresh(httplib2.Http())
    return credentials


def print_creds(credentials):
    '''HTTPリクエスト中で使用する許可ヘッダを出力する。'''
    cred_dict = json.loads(credentials.to_json())
    if 'access_token' in cred_dict:
        print 'Authorization: Bearer %s' % (cred_dict['access_token'],)
    else:
        print 'creds: %s' % (cred_dict,)


def main():
    print_creds(get_oauth_creds())

if __name__ == "__main__":
    main()

参考

yukihirop
気の向くまま。意の向くままにコードを書くプログラマー。 役に立つツールを作るのって本当に難しい。
https://creator-of-what.yukihirop.me/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした