LoginSignup
4
5

More than 5 years have passed since last update.

PythonでGoogleアカウントの個人情報を取得する最も簡単な方法

Posted at

はじめに

Google認証をしていると、各ユーザーがGoogleに登録したプロフィール情報を取得し、何らかの分析を行いたいことがあると思います。今回はPython環境でのやり方を説明していきます。公式のやり方はありますが、少し面倒ですし、数行でできた方がいいじゃないですかってことで。

  • 環境
    • Python3.6.4

結果

$ python googleplus.py
{'kind': 'plus#person', 'etag': '"RKS4-q7QGL10FxltAebpjqjKQR0/Ci9guzYOcHZNK1zsbFrGae0BbDg"', 'occupation': 'Head of Develope
r Relations @ Crate.IO. Former Twitter, Google & Founder.', 'birthday': '0000-08-30', 'gender': 'male', 'urls': [{'value': '
http://www.snapsation.com', 'type': 'otherProfile', 'label': 'Snapsation'}, {'value': 'https://plus.google.com/b/10899540122
5573100648/108995401225573100648/posts', 'type': 'otherProfile', 'label': 'Snapsation Google+ Page'}, {'value': 'http://www.
linkedin.com/in/chabotc', 'type': 'otherProfile', 'label': 'in/chabotc'}, {'value': 'http://www.facebook.com/chabotc', 'type
': 'otherProfile', 'label': 'facebook.com/chabotc'}, {'value': 'http://www.500px.com/chabotc', 'type': 'otherProfile', 'labe
l': '500px'}, {'value': 'http://www.flickr.com/chrischabot', 'type': 'otherProfile', 'label': 'Flickr'}, {'value': 'http://w
ww.youtube.com/watch?v=ZLoyoFqv4PQ', 'type': 'other', 'label': 'Welcome to Snapsation!'}, {'value': 'https://plus.google.com
/108189587050871927619/posts/Z4eZiEaVg4z', 'type': 'other', 'label': 'Tutorial: Replacing the sky in Photoshop'}, {'value':
'http://www.youtube.com/watch?v=KIPOU-Et5Ts', 'type': 'other', 'label': 'Canon ES 24-70 F/4.0 IS USM Review'}], 'objectType'
: 'person', 'id': '108189587050871927619', 'displayName': 'Chris Chabot', 'name': {'familyName': 'Chabot', 'givenName': 'Chr
is'}, 'tagline': 'Distilling fact from the vapors of nuance', 'aboutMe': '<p>I’m driven by a passion for societal change th
rough technological innovation. With experience working at Google and Twitter, leading developer and consumer product teams
reaching millions of users, founding and advising startups and as frequent keynote speaker. I’m an innovator that translate
s technological innovations into behavioral change and user focused product design.</p>', 'url': 'https://plus.google.com/+C
hrisChabot', 'image': {'url': 'https://lh5.googleusercontent.com/-cQNLOQzkGpE/AAAAAAAAAAI/AAAAAAABja0/D3xjB7TqgA8/photo.jpg?
sz=50', 'isDefault': False}, 'organizations': [{'name': 'Amazon', 'title': 'Technical Evangelist', 'type': 'work', 'startDat
e': '2017', 'endDate': '2017', 'primary': False}, {'name': 'Fiedler Capital', 'title': 'Chief Program Officer', 'type': 'wor
k', 'startDate': '2014', 'endDate': '2016', 'primary': False}, {'name': 'Twitter Inc.', 'title': 'Head of International Deve
loper & Platform Relations', 'type': 'work', 'startDate': '2013', 'endDate': '2014', 'primary': False}, {'name': 'Google', '
title': 'Head of Google+ Developer Relations', 'type': 'work', 'startDate': '2007', 'endDate': '2013', 'primary': False}, {'
name': 'Resin.io', 'title': 'Head of Developer Relations', 'type': 'work', 'startDate': '2018', 'primary': True}], 'placesLi
ved': [{'value': 'London, UK', 'primary': True}, {'value': 'San Francisco, CA, USA'}, {'value': 'Rotterdam, The Netherlands'
}, {'value': 'Amsterdam, The Netherlands'}, {'value': 'Dallas, Texas'}, {'value': 'Cambridge, UK'}, {'value': 'Rotterdam, Th
e Netherlands'}], 'isPlusUser': True, 'circledByCount': 164188, 'verified': True, 'cover': {'layout': 'banner', 'coverPhoto'
: {'url': 'https://lh3.googleusercontent.com/mKGWrBb-_AZkFJQ418sQ1U0lYG_Gytn3c7A060AEpCwKGKFPheciKBVrA_Eex7Oi7kAiFCcBX9VO=s6
30-fcrop64=1,000029c1feabffff', 'height': 626, 'width': 940}, 'coverInfo': {'topImageOffset': -271, 'leftImageOffset': 0}}}

準備

インストール

$ pip install requests

認証情報とGoogle+ APIの有効化

  1. Google API Console へ移動します。
  2. [作成]ボタンを押してプロジェクトを作成してください。
  3. プロジェクト名は好きなプロジェクト名で良いですが、ここでは「My Project」とします。
  4. [作成]ボタンを押します。
  5. ライブラリ選択画面にて、Google+ API を選択 [有効にする]を押します。
  6. [左上の三本線のナビゲーションメニュー]->[APIとサービス]->[認証情報]をクリックします。
  7. 認証情報タブにて、[認証情報を作成]->[APIキー]を押します。

コーディング

基本的な方針は以下の通りです。

  1. URL を決定
  2. GET リクエストを送信
  3. レスポンス内容を json 形式に変換
googleplus.py
import requests

DEVELOPER_KEY = '' # 取得したデベロッパーキー
USER_ID = '108189587050871927619' # プロフィール情報を取得したい人のユーザID(例はよく使われるChris氏のユーザID)


# webAPIからJSONの形式の文字列の結果をもらう
def get_profile():

    # URIスキーム
    url = 'https://www.googleapis.com/plus/v1/people/' + USER_ID +'?key=' + DEVELOPER_KEY

    # webAPIからのリクエストを送信
    response = requests.get(url)

    # json に変換
    json_data = response.json()

    return json_data


if __name__ == '__main__':

    user_prof = get_profile()
    print(user_prof)

おわりに

今回は最も簡単な方法ということで、他の要素を極限まで排除しました。開発者の人脈が欲しいので、もしよろしければ Twitter の方もフォローいただけると幸いです。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5