示例#1
0
    def get_access_token(self, installation_id, user_id=None):
        """
        Get an access token for the given installation id.
        POSTs https://api.github.com/installations/<installation_id>/access_tokens
        :param user_id: int
        :param installation_id: int
        :return: :class:`github.InstallationAuthorization.InstallationAuthorization`
        """
        body = None
        if user_id:
            body = json.dumps({"user_id": user_id})
        conn = HTTPSConnection("api.github.com")
        conn.request(
            method="POST",
            url="/installations/{}/access_tokens".format(installation_id),
            headers={
                "Authorization": "Bearer {}".format(self.create_jwt()),
                "Accept": "application/vnd.github.machine-man-preview+json",
                "User-Agent": "PyGithub/Python"
            },
            body=body
        )
        response = conn.getresponse()
        response_text = response.read()

        if atLeastPython3:
            response_text = response_text.decode('utf-8')

        conn.close()
        if response.status == 201:
            data = json.loads(response_text)
            return InstallationAuthorization.InstallationAuthorization(
                requester=None,  # not required, this is a NonCompletableGithubObject
                headers={},  # not required, this is a NonCompletableGithubObject
                attributes=data,
                completed=True
            )
        elif response.status == 403:
            raise GithubException.BadCredentialsException(
                status=response.status,
                data=response_text
            )
        elif response.status == 404:
            raise GithubException.UnknownObjectException(
                status=response.status,
                data=response_text
            )
        raise GithubException.GithubException(
            status=response.status,
            data=response_text
        )
示例#2
0
    def get_access_token(self, installation_id, user_id=None):
        """
        Get an access token for the given installation id.
        POSTs https://api.github.com/installations/<installation_id>/access_tokens
        :param user_id: int
        :param installation_id: int
        :return: :class:`github.InstallationAuthorization.InstallationAuthorization`
        """
        body = None
        if user_id:
            body = json.dumps({"user_id": user_id})
        conn = HTTPSConnection("api.github.com")
        conn.request(
            method="POST",
            url="/installations/{}/access_tokens".format(installation_id),
            headers={
                "Authorization": "Bearer {}".format(self.create_jwt()),
                "Accept": "application/vnd.github.machine-man-preview+json",
                "User-Agent": "PyGithub/Python"
            },
            body=body
        )
        response = conn.getresponse()
        response_text = response.read()

        if atLeastPython3:
            response_text = response_text.decode('utf-8')

        conn.close()
        if response.status == 201:
            data = json.loads(response_text)
            return InstallationAuthorization.InstallationAuthorization(
                requester=None,  # not required, this is a NonCompletableGithubObject
                headers={},  # not required, this is a NonCompletableGithubObject
                attributes=data,
                completed=True
            )
        elif response.status == 403:
            raise GithubException.BadCredentialsException(
                status=response.status,
                data=response_text
            )
        elif response.status == 404:
            raise GithubException.UnknownObjectException(
                status=response.status,
                data=response_text
            )
        raise GithubException.GithubException(
            status=response.status,
            data=response_text
        )
示例#3
0
    def get_access_token(self, installation_id, user_id=None):
        """
        Get an access token for the given installation id.
        POSTs https://api.github.com/installations/<installation_id>/access_tokens
        :param user_id: int
        :param installation_id: int
        :return: :class:`github.InstallationAuthorization.InstallationAuthorization`
        """
        body = None
        if user_id:
            body = json.dumps({"user_id": user_id})
        response = requests.post("https://api.github.com/installations/{}/access_tokens".format(installation_id),
                                 headers={
                                     "Authorization": "Bearer {}".format(self.create_jwt()),
                                     "Accept": Consts.mediaTypeIntegrationPreview,
                                     "User-Agent": "PyGithub/Python"
                                 },
                                 body=body)
        response_text = response.text

        if atLeastPython3:
            response_text = response_text.decode('utf-8')

        if response.status_code == 201:
            data = json.loads(response_text)
            return InstallationAuthorization.InstallationAuthorization(
                requester=None,  # not required, this is a NonCompletableGithubObject
                headers={},  # not required, this is a NonCompletableGithubObject
                attributes=data,
                completed=True
            )
        elif response.status_code == 403:
            raise GithubException.BadCredentialsException(
                status=response.status_code,
                data=response_text
            )
        elif response.status_code == 404:
            raise GithubException.UnknownObjectException(
                status=response.status_code,
                data=response_text
            )
        raise GithubException.GithubException(
            status=response.status_code,
            data=response_text
        )