Skip to main content

Python library for mixi Graph API

Project description

概要

mixi Graph API を Python から簡単に使えるようにするためのライブラリです。

インストール方法

easy_install を使ってインストールします。依存ライブラリの MultipartPostHandler も自動的にインストールされます。

# easy_install strippers.mixi

サポートしているAPI

現在このライブラリがサポートしている mixi Graph API は以下の通りです。

People API

READ_PROFILE (r_profile) スコープ

  • 友人一覧の取得
    • get_friends(group_id=’@friends’, sort_by=None, sort_order=’ascending’, count=20, start_index=0)

Voice API

READ_VOICE (r_voice) スコープ

WRITE_VOICE (w_voice) スコープ

Message API

READ_MESSAGE (r_message) スコープ

WRITE_MESSAGE (w_message) スコープ

使用方法

MixiGraphAPI オブジェクトの初期化

>>> from strippers.mixi import MixiGraphAPI, WRITE_VOICE, DEVICE_PC
>>>
>>> # mixi サービス登録情報を定義
>>> consumer_key = 'xxxxxxxxxxxxxxxxxxxx'
>>> consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
>>> redirect_uri = 'http://www.example.com/mixi/authorized'
>>>
>>> # 使用する API のスコープを指定
>>> scopes = [WRITE_VOICE]
>>>
>>> # (1) MixiGraphAPI オブジェクトを生成
>>> api = MixiGraphAPI(consumer_key, consumer_secret, scopes)
>>>
>>> # (2) ユーザ認可用 URL を取得
>>> api.get_auth_url(device=DEVICE_PC)
'https://mixi.jp/connect_authorize.pl?scope=w_voice&response_type=code&client_id=xxxxxxxxxxxxxxxxxxxx&display=pc'

>>> # (3) 上の URL にアクセスして[同意する]と、サービス登録した redirect_uri に
>>> # code パラメータ付きでリダイレクトされてきます。
>>> # この code パラメータを利用します。
>>> code = '5fbf8d9e55f0df0deff68cd4a8500701b3fe6baa'
>>>
>>> # (4) MixiGraphAPI オブジェクトを初期化
>>> api.initialize(code, redirect_uri)
>>>
>>> # 認可ユーザのアクセストークンとリフレッシュトークンを取得
>>> access_token, refresh_token = api.tokens

MixiGraphAPI オブジェクトを初期化すると、各 API メソッドを実行できるようになります。

ユーザのアクセストークンとリフレッシュトークンが予め分かっている場合は、上記(1)~(4)の MixiGraphAPI オブジェクトの初期化処理は要らず、次のようにアクセストークンとリフレッシュトークンを引数に渡して MixiGraphAPI オブジェクトを生成するだけで済みます。

>>> api = MixiGraphAPI(consumer_key, consumer_secret, scopes, access_token, refresh_token)

つぶやきの投稿

>>> status = api.update_status('投稿なう')
>>>
>>> # 上のつぶやきを削除します
>>> api.delete_status(status['id'])

フォト付きでつぶやきを投稿するには、画像ファイルのファイルパス、または「http(s)://」で始まる URL を第2引数に渡します。

>>> api.update_status('フォト投稿なう', '/path/to/picture.jpg')
>>>
>>> # フォトだけの投稿も可能
>>> api.update_status(photo='/path/to/picture.jpg')

トークンの有効期限切れ

アクセストークン、リフレッシュトークンそれぞれに有効期限があります。アクセストークンの有効期限は約15分ですが、リフレッシュトークンが有効期限内の間は MixiGraphAPI オブジェクト内部で自動的に再取得(リフレッシュ)します。

リフレッシュトークンの有効期限は約6時間(ユーザ認可時に「常に同意する」のチェックを入れていれば約3ヶ月間)です。リフレッシュトークンが期限切れになった場合は、MixiGraphAPI オブジェクトの初期化処理が必要になります。つまり、再度ユーザ認可を行います。

>>> from strippers.mixi import ExpiredTokenException
>>>
>>> try:
>>>     api.update_status('リフレッシュトークン期限切れの場合')
>>> except ExpiredTokenException:
>>>     auth_url = api.get_auth_url(device=DEVICE_PC)
>>>     # auth_url にアクセスしてユーザ認可処理...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strippers.mixi-1.0a5-py2.6.egg (22.9 kB view details)

Uploaded Egg

File details

Details for the file strippers.mixi-1.0a5-py2.6.egg.

File metadata

File hashes

Hashes for strippers.mixi-1.0a5-py2.6.egg
Algorithm Hash digest
SHA256 0f416a0f2241a8245f138b6814a2b546840eff5b722ef2f8327debc6952c9e13
MD5 7f91135539c9cbada126e9ee2a31095a
BLAKE2b-256 ec952ccf80d9ceb9ac5a47a65210568fd1da19cb4ebd4d98f774fa458a1d30be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page