Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/backends/qiita.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Qiita
=====

Qiita

- Register a new application at `https://qiita.com/settings/applications`_, set the
callback URL to ``http://example.com/complete/qiita/`` replacing
``example.com`` with your domain.

- Fill ``Client ID`` and ``Client Secret`` values in the settings::

SOCIAL_AUTH_QIITA_KEY = ''
SOCIAL_AUTH_QIITA_SECRET = ''

- Also it's possible to define extra permissions with::

SOCIAL_AUTH_QIITA_SCOPE = [...]

See auth scopes at https://qiita.com/api/v2/docs#スコープ
66 changes: 66 additions & 0 deletions social/backends/qiita.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Qiita OAuth2 backend, docs at:
http://psa.matiasaguirre.net/docs/backends/qiita.html
http://qiita.com/api/v2/docs#get-apiv2oauthauthorize
"""
import json

from social.backends.oauth import BaseOAuth2


class QiitaOAuth2(BaseOAuth2):
"""Qiita OAuth authentication backend"""
name = 'qiita'

AUTHORIZATION_URL = 'https://qiita.com/api/v2/oauth/authorize'
ACCESS_TOKEN_URL = 'https://qiita.com/api/v2/access_tokens'
ACCESS_TOKEN_METHOD = 'POST'
SCOPE_SEPARATOR = ' '
REDIRECT_STATE = True
EXTRA_DATA = [
('description', 'description'),
('facebook_id', 'facebook_id'),
('followees_count', 'followees_count'),
('followers_count', 'followers_count'),
('github_login_name', 'github_login_name'),
('id', 'id'),
('items_count', 'items_count'),
('linkedin_id', 'linkedin_id'),
('location', 'location'),
('name', 'name'),
('organization', 'organization'),
('profile_image_url', 'profile_image_url'),
('twitter_screen_name', 'twitter_screen_name'),
('website_url', 'website_url'),
]

def auth_complete_params(self, state=None):
data = super(QiitaOAuth2, self).auth_complete_params(state)
if "grant_type" in data:
del data["grant_type"]
if "redirect_uri" in data:
del data["redirect_uri"]
return json.dumps(data)

def auth_headers(self):
return {'Content-Type': 'application/json'}

def request_access_token(self, *args, **kwargs):
data = super(QiitaOAuth2, self).request_access_token(*args, **kwargs)
data.update({'access_token': data['token']})
return data

def get_user_details(self, response):
"""Return user details from Qiita account"""
return {
'username': response['id'],
'fullname': response['name'],
}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
return self.get_json(
'https://qiita.com/api/v2/authenticated_user',
headers={
'Authorization': ' Bearer {0}'.format(access_token)
})
25 changes: 25 additions & 0 deletions social/tests/backends/test_qiita.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json

from social.tests.backends.oauth import OAuth2Test


class QiitaOAuth2Test(OAuth2Test):
backend_path = 'social.backends.qiita.QiitaOAuth2'
user_data_url = 'https://qiita.com/api/v2/authenticated_user'
expected_username = 'foobar'

access_token_body = json.dumps({
'token': 'foobar',
'token_type': 'bearer'
})

user_data_body = json.dumps({
'id': 'foobar',
'name': 'Foo Bar'
})

def test_login(self):
self.do_login()

def test_partial_pipeline(self):
self.do_partial_pipeline()