yarbshk / pybitrix24

The simplest zero dependency polyversion Python library for Bitrix24 REST API.
MIT License
43 stars 23 forks source link

Oauth refresh tokens for Box version #3

Closed nxexox closed 5 years ago

nxexox commented 5 years ago

Please change Bitrix24 domain for oauth refresh tokens for Box version. https://dev.1c-bitrix.ru/learning/course/?COURSE_ID=99&LESSON_ID=5074 It is described in more detail. It is necessary for the tokens to change the domain to oauth.bitrix.info. I checked, and the truth for the boxed version only works like this.

nxexox commented 5 years ago

Example:

class Bitrix24Client(Bitrix24):
    """
    Custom Bitrix24 client for Custom oauth urls.

    """
    _oauth_endpoint_template = 'https://{domain}/oauth/{action}/'
    _oauth_endpoint_template_tokens = 'https://oauth.bitrix.info/oauth/{action}/'

    def _resolve_oauth_endpoint(self, action, query=None, tokens: bool = False) -> str:
        """
        Builds an OAuth URL with/out query parameters.

        :param str action: str Action name of an OAuth endpoint
        :param dict query: dict Query parameters
        :param bool tokens: Tokens oauth point?

        :return: str OAuth endpoint
        :rtype: str

        """
        template = self._oauth_endpoint_template
        if tokens:
            template = self._oauth_endpoint_template_tokens

        endpoint = template.format(
            domain=self.domain,
            action=action
        )
        if query is not None:
            endpoint += '?{}'.format(urlencode(query))
        return endpoint

    def _request_tokens(self, query) -> dict:
        """
        The request handler of an OAuth tokens endpoint.

        :param dict query: dict Query parameters

        :return: dict Encoded response text
        :rtype: dict

        """
        url = self._resolve_oauth_endpoint('token', tokens=True)
        try:
            r = requests.get(url, params=query)
        except requests.exceptions.RequestException:
            r = None
        result = utils.resolve_response(r)
        return result
yarbshk commented 5 years ago

hi @nxexox . thanks for your contribution! please download latest version: $ pip install bitrix24-python3-client==0.3.4

...and manually add the authentication server hostname as follows during instantiation of a client:

>>> from bitrix24 import Bitrix24
>>> bx24 = Bitrix24('your-domain.bitrix24.com', 'your.client.id', 'your_client_secret', auth_domain='oauth.bitrix.info')

hope it helps. close the issue

nxexox commented 5 years ago

Ok, I'll try. Don't you upload it to the git repository yet?

yarbshk commented 5 years ago

sure. i've just upload the latest changes on github

nxexox commented 5 years ago

👍 thanks

nxexox commented 5 years ago

Refresh REAME.md please.