Esempio n. 1
0
def update_token(client: OAuthClient) -> None:
    client.refresh()
    with open('tokens.json', 'w') as f:
        json.dump(
            {
                'access_token': client.access_token,
                'refresh_token': client.refresh_token,
            },
            f,
            indent=2)
Esempio n. 2
0
def forcoin(request):
    ClientId = "77d5af4941ffd6d5f0c9149e2e303a1c28d9f44c09b63694d6f2f608c60947c0"
    Clientsecret = "4395c36b93aa972ab4c6da2278cdca48f93e8551d0b13ee502552e249db90eb4"
    code = request.GET.get('code', '')
    r2 = requests.post("https://api.coinbase.com/oauth/token",
                       data={
                           "grant_type": "authorization_code",
                           "code": code,
                           "client_id": ClientId,
                           "client_secret": Clientsecret,
                           "redirect_uri": "http://127.0.0.1:8000/Ali/forcoin/"
                       })
    r3 = r2.json()
    a = OAuthClient(r3['access_token'], r3['refresh_token'])
    b = a.get_spot_price().amount
    c = a.get_historic_prices(currency_pair='BTC-USD', period='day')['prices']
    user = a.get_current_user().name
    useremail = a.get_current_user().email
    access = a.refresh()['access_token']
    refresh = a.refresh()['refresh_token']

    price = []
    price2 = []
    time = []
    dif1 = []

    for i in range(1, 21):
        price.append(float(c[i]['price']))
        price2.append(float(c[i - 1]['price']))
        dif1.append(round(price[i - 1] - price2[i - 1], 2))
        time.append(c[i]['time'])

    coinpt = zip(price, dif1, time)

    context = {
        'code': code,
        'price': b,
        'coinprice': price,
        'time': time,
        'coinpt': coinpt,
        'user': user,
        'useremail': useremail,
        'access': access,
        'refresh': refresh,
    }

    return render(request, 'Ali/forcoin.html', context)
Esempio n. 3
0
def get_oauth_client(access_token, refresh_token, user=None):
    oauth_client = OAuthClient(access_token, refresh_token, base_api_uri=COINBASE_BASE_API_URL)
    new_credentials = oauth_client.refresh()
    if user:
        defaults = {
            'token': new_credentials['access_token'],
            'token_secret': new_credentials['refresh_token'],
            # 'expires_at': response['expires_in'] # Coinbase returns an interval here
        }
        BTCWallet.objects.update_or_create(
            user=user, provider=BTC_WALLET_PROVIDER_COINBASE, defaults=defaults
        )
    return oauth_client
    def test_refresh(self):
        # Requests to the default BASE_API_URI will noticeably fail by raising an
        # AssertionError. Requests to the new URL will respond HTTP 200.
        new_api_base = 'http://example.com/'

        # If any error is raised by the server, the test suite will never exit when
        # using Python 3. This strange technique is used to raise the errors
        # outside of the mocked server environment.
        errors_in_server = []

        server_response_data = {
            'access_token': 'newaccesstoken',
            'refresh_token': 'newrefreshtoken',
        }

        def server_response(request, uri, headers):
            parsed_uri = urlparse(uri)
            parsed_reference = urlparse(new_api_base)
            try:
                self.assertEqual(parsed_uri.scheme, parsed_reference.scheme)
                self.assertEqual(parsed_uri.netloc, parsed_reference.netloc)
                self.assertEqual(parsed_uri.path, parsed_reference.path)
            except AssertionError as e:
                errors_in_server.append(e)
            return (200, headers, json.dumps(server_response_data))

        hp.register_uri(hp.POST,
                        OAuthClient.BASE_API_URI + 'oauth/token',
                        body=server_response)
        hp.register_uri(hp.POST,
                        new_api_base + 'oauth/token',
                        body=server_response)

        client = OAuthClient(access_token, refresh_token)
        with self.assertRaises(AssertionError):
            client.refresh()
            if errors_in_server:
                raise errors_in_server.pop()

        client2 = OAuthClient(access_token,
                              refresh_token,
                              base_api_uri=new_api_base)
        self.assertEqual(client2.refresh(), server_response_data)

        # If the response does not include both an access token and refresh token,
        # an exception will be raised.
        server_response_data = {'access_token': 'someaccesstoken'}
        with self.assertRaises(APIError):
            client2.refresh()
        server_response_data = {'refresh_token': 'somerefreshtoken'}
        with self.assertRaises(APIError):
            client2.refresh()
def get_oauth_client(access_token, refresh_token, user=None):
    oauth_client = OAuthClient(access_token,
                               refresh_token,
                               base_api_uri=COINBASE_BASE_API_URL)
    new_credentials = oauth_client.refresh()
    if user:
        defaults = {
            'token': new_credentials['access_token'],
            'token_secret': new_credentials['refresh_token'],
            # 'expires_at': response['expires_in'] # Coinbase returns an interval here
        }
        BTCWallet.objects.update_or_create(
            user=user,
            provider=BTC_WALLET_PROVIDER_COINBASE,
            defaults=defaults)
    return oauth_client
Esempio n. 6
0
    def test_refresh(self):
        # Requests to the default BASE_API_URI will noticeably fail by raising an
        # AssertionError. Requests to the new URL will respond HTTP 200.
        new_api_base = 'http://example.com/'

        # If any error is raised by the server, the test suite will never exit when
        # using Python 3. This strange technique is used to raise the errors
        # outside of the mocked server environment.
        errors_in_server = []

        server_response_data = {
            'access_token': 'newaccesstoken',
            'refresh_token': 'newrefreshtoken',
        }

        def server_response(request, uri, headers):
            parsed_uri = urlparse(uri)
            parsed_reference = urlparse(new_api_base)
            try:
                self.assertEqual(parsed_uri.scheme, parsed_reference.scheme)
                self.assertEqual(parsed_uri.netloc, parsed_reference.netloc)
                self.assertEqual(parsed_uri.path, parsed_reference.path)
            except AssertionError as e:
                errors_in_server.append(e)
            return (200, headers, json.dumps(server_response_data))
        hp.register_uri(hp.POST, OAuthClient.BASE_API_URI + 'oauth/token', body=server_response)
        hp.register_uri(hp.POST, new_api_base + 'oauth/token', body=server_response)

        client = OAuthClient(access_token, refresh_token)
        with self.assertRaises(AssertionError):
            client.refresh()
            if errors_in_server:
                raise errors_in_server.pop()

        client2 = OAuthClient(
            access_token,
            refresh_token,
            base_api_uri=new_api_base)
        self.assertEqual(client2.refresh(), server_response_data)

        # If the response does not include both an access token and refresh token,
        # an exception will be raised.
        server_response_data = {'access_token': 'someaccesstoken'}
        with self.assertRaises(APIError):
            client2.refresh()
        server_response_data = {'refresh_token': 'somerefreshtoken'}
        with self.assertRaises(APIError):
            client2.refresh()
Esempio n. 7
0
def get_new_token():
    for i in range(0,len(user_token.objects.all())):
        a=OAuthClient(user_token.objects.values()[i]['access_token'],user_token.objects.values()[i]['refresh_token'])
        a=a.refresh()
        user_token.objects.filter(user_id=user_token.objects.values()[i]['user_id']).update(access_token=a['access_token'],refresh_token=a['refresh_token'])