From 17aae466d36f32abc725415f171ee81d3699a52f Mon Sep 17 00:00:00 2001 From: Jakob Linskeseder Date: Thu, 11 Feb 2021 20:30:19 +0100 Subject: [PATCH] Add support for device-code authorization --- CHANGELOG.md | 10 +++++++++- setup.py | 2 +- vimeo/auth/device_code.py | 37 +++++++++++++++++++++++++++++++++++++ vimeo/client.py | 5 +++-- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 vimeo/auth/device_code.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e7d006..9e5be9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog -## [1.1.0] - 2018-05-20 +## [1.2.0] - 2021-02-12 +### Added +- Added support for device-code authorization + +## [1.1.0] - 2020-05-20 +### Changed +- Removed Python 2.7 support + ### Fixed - Add back missing classifiers in `setup.py` @@ -124,6 +131,7 @@ additional issues are invited for full support. ### Added - First release using the Python [requests](http://docs.python-requests.org/en/latest/) module +[1.2.0]: https://github.com/vimeo/vimeo.py/compare/1.1.0...1.2.0 [1.1.0]: https://github.com/vimeo/vimeo.py/compare/1.0.11...1.1.0 [1.0.11]: https://github.com/vimeo/vimeo.py/compare/1.0.10...1.0.11 [1.0.10]: https://github.com/vimeo/vimeo.py/compare/1.0.9...1.0.10 diff --git a/setup.py b/setup.py index d95631f..c6bf53b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup(name='PyVimeo', - version='1.1.0', + version='1.2.0', description='Simple interaction with the Vimeo API.', url='https://developer.vimeo.com/', author='Vimeo', diff --git a/vimeo/auth/device_code.py b/vimeo/auth/device_code.py new file mode 100644 index 0000000..baec474 --- /dev/null +++ b/vimeo/auth/device_code.py @@ -0,0 +1,37 @@ +#! /usr/bin/env python +# encoding: utf-8 + +from .base import AuthenticationMixinBase +from . import GrantFailed + + +class DeviceCodeMixin(AuthenticationMixinBase): + """Implement helpers for the Device Code grant for OAuth2.""" + + def load_device_code(self, scope): + """Perform the request for device code.""" + code, headers, resp = self.call_grant( + "/oauth/device", { + "grant_type": "device_grant", + "scope": scope + }) + + if not code == 200: + raise GrantFailed() + + return resp + + def device_code_authorize(self, user_code, device_code): + """Perform the authorization step after the user entered the device code.""" + code, headers, resp = self.call_grant( + "/oauth/device/authorize", { + "user_code": user_code, + "device_code": device_code + }) + + if not code == 200: + raise GrantFailed() + + self.token = resp["access_token"] + + return self.token, resp["user"], resp["scope"] diff --git a/vimeo/client.py b/vimeo/client.py index 249bc8b..483aad6 100644 --- a/vimeo/client.py +++ b/vimeo/client.py @@ -6,17 +6,18 @@ import requests from .auth.client_credentials import ClientCredentialsMixin from .auth.authorization_code import AuthorizationCodeMixin +from .auth.device_code import DeviceCodeMixin from .upload import UploadMixin from .exceptions import APIRateLimitExceededFailure -class VimeoClient(ClientCredentialsMixin, AuthorizationCodeMixin, UploadMixin): +class VimeoClient(ClientCredentialsMixin, AuthorizationCodeMixin, DeviceCodeMixin, UploadMixin): """Client handle for the Vimeo API.""" API_ROOT = "https://api.vimeo.com" HTTP_METHODS = {'head', 'get', 'post', 'put', 'patch', 'options', 'delete'} ACCEPT_HEADER = "application/vnd.vimeo.*;version=3.4" - USER_AGENT = "pyvimeo 1.0.11; (http://developer.vimeo.com/api/docs)" + USER_AGENT = "pyvimeo 1.2.0; (http://developer.vimeo.com/api/docs)" def __init__(self, token=None, key=None, secret=None, *args, **kwargs): """Prep the handle with the authentication information."""