Exemplo n.º 1
0
    def create_from_authorization_code(klass, authorization_code, client_id, client_secret, redirect_uri):

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        params = urllib.urlencode({
            'client_id' : client_id,
            'client_secret' : client_secret,
            'code' : authorization_code,
            'redirect_uri' : redirect_uri,
            'grant_type' : 'authorization_code'
        })

        conn = VerifiedHTTPSConnection(klass.TOKEN_DOMAIN)
        conn.request("POST", klass.TOKEN_URL, params, headers)

        response = conn.getresponse()
        data = response.read()
        refresh_token = None
        access_token = None
        if response.status == 200:
            value = json.loads(data)
            access_token = value['access_token']
            refresh_token = value['refresh_token']
        else:
            dict = { 'status' : response.status }
            try:
                j = json.loads(data)
                dict.update(j)
            except:
                dict['data'] = data
            raise RequestError(dict)

        cls = klass(refresh_token, client_id, client_secret)
        cls.access_token = access_token
        return cls
Exemplo n.º 2
0
    def update_access_token(self):
        args = self.base_auth_args.copy()
        args['grant_type'] = 'refresh_token'
        args['refresh_token'] = self.refresh_token
        params = urllib.urlencode(args)

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        conn = VerifiedHTTPSConnection(self.TOKEN_DOMAIN)
        conn.request("POST", self.TOKEN_URL, params, headers)

        response = conn.getresponse()
        data = response.read()
        val = None
        if response.status == 200:
            try:
                value = json.loads(data)
                val = value['access_token']
            except:
                pass

        self._access_token = val
Exemplo n.º 3
0
    def request(self, method, url, body=None, headers={}, expected_resp=200, allow_new_access_token=True):
        if not headers:
            headers = {}

        headers['Authorization'] = 'OAuth %s' % self.access_token

        conn = VerifiedHTTPSConnection(self.DOMAIN)
        conn.request(method, url, body, headers)

        response = conn.getresponse()
        if response.status == 401 and allow_new_access_token:
            self.update_access_token()
            return self.request(method, url, body, headers, expected_resp, False)

        try:
            data = response.read()
            if response.status == expected_resp:
                return data
            else:
                dict = { 'status' : response.status, 'raw' : data}
                try:
                    j = json.loads(data)
                    dict.update(j)
                except:
                    dict['data'] = data
                raise RequestError(dict)
        finally:
            conn.close()
Exemplo n.º 4
0
    def create_from_authorization_code(klass, authorization_code, client_id,
                                       client_secret, redirect_uri):

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        params = urllib.urlencode({
            'client_id' : client_id,
            'client_secret' : client_secret,
            'code' : authorization_code,
            'redirect_uri' : redirect_uri,
            'grant_type' : 'authorization_code'
        })

        conn = VerifiedHTTPSConnection(klass.TOKEN_DOMAIN)
        conn.request("POST", klass.TOKEN_URL, params, headers)

        response = conn.getresponse()
        data = response.read()
        refresh_token = None
        access_token = None
        if response.status == 200:
            value = json.loads(data)
            access_token = value['access_token']
            refresh_token = value.get('refresh_token')
        else:
            dict = { 'status' : response.status }
            try:
                j = json.loads(data)
                dict.update(j)
            except:
                dict['data'] = data
            raise RequestError(dict)

        token = access_token
        if refresh_token:
            token = refresh_token
        cls = klass(token, client_id, client_secret)
        cls.access_token = access_token
        return cls
Exemplo n.º 5
0
    def request(self, method, url, body=None, headers={},
                expected_resp=200, allow_new_access_token=True):
        if not headers:
            headers = {}

        headers['Authorization'] = 'OAuth %s' % self.access_token

        conn = VerifiedHTTPSConnection(self.DOMAIN)
        conn.request(method, url, body, headers)

        response = conn.getresponse()
        if response.status == 401 and allow_new_access_token:
            self.update_access_token()
            return self.request(
                method,
                url,
                body,
                headers,
                expected_resp,
                False,
            )

        try:
            data = response.read()
            if response.status == expected_resp:
                return data
            else:
                dict = { 'status' : response.status, 'raw' : data}
                try:
                    j = json.loads(data)
                    dict.update(j)
                except:
                    dict['data'] = data
                raise RequestError(dict)
        finally:
            conn.close()