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

strippers.mixi-1.0a3-py2.6.egg (16.8 kB view hashes)

Uploaded Source

Supported by

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