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) スコープ

Voice API

READ_VOICE (r_voice) スコープ

WRITE_VOICE (w_voice) スコープ

使用方法

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(phpoto='/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.0a3-py2.6.egg (16.8 kB view details)

Uploaded Egg

File details

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

File metadata

File hashes

Hashes for strippers.mixi-1.0a3-py2.6.egg
Algorithm Hash digest
SHA256 a4fcbfc063868c4d29c8d67065ef5567a3561e0aa34b40250be724f325144d73
MD5 3fcfdeb20d1913ca43a8523f974cdd56
BLAKE2b-256 515186c599546f49a93923db96529f003ee532346318efdda5ac7f771edacb89

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