adding token_refresh, trying get_contacts - not working
This commit is contained in:
@@ -3,34 +3,116 @@ import json
|
||||
from requests.auth import HTTPBasicAuth
|
||||
import urllib.parse
|
||||
from rowers.utils import dologging
|
||||
from rowers.models import FakturoidToken
|
||||
import base64
|
||||
from django.utils import timezone
|
||||
import datetime
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
FAKTUROID_EMAIL, FAKTUROID_API_KEY,
|
||||
FAKTUROID_SLUG
|
||||
FAKTUROID_SLUG, FAKTUROID_CLIENT_ID, FAKTUROID_CLIENT_SECRET,
|
||||
FAKTUROID_APP_NAME
|
||||
)
|
||||
|
||||
slug = FAKTUROID_SLUG
|
||||
|
||||
invoices_url = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/invoices.json'.format(
|
||||
invoices_url = 'https://app.fakturoid.cz/api/v3/accounts/{slug}/invoices.json'.format(
|
||||
slug=slug)
|
||||
contacts_url = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/subjects.json'.format(
|
||||
contacts_url = 'https://app.fakturoid.cz/api/v3/accounts/{slug}/subjects.json'.format(
|
||||
slug=slug)
|
||||
contacts_search_url = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/subjects/search.json'.format(
|
||||
contacts_search_url = 'https://app.fakturoid.cz/api/v3/accounts/{slug}/subjects/search.json'.format(
|
||||
slug=slug)
|
||||
|
||||
token_url = 'https://app.fakturoid.cz/api/v3/oauth/token'
|
||||
|
||||
auth = HTTPBasicAuth(FAKTUROID_EMAIL, FAKTUROID_API_KEY)
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': 'rowsandall (admin@rowsandall.com)'
|
||||
'User-Agent': f"{FAKTUROID_APP_NAME} ({FAKTUROID_EMAIL})",
|
||||
}
|
||||
|
||||
def fakturoid_token_refresh():
|
||||
try:
|
||||
token = FakturoidToken.objects.get(id=1)
|
||||
except FakturoidToken.DoesNotExist:
|
||||
return 0
|
||||
|
||||
refreshtoken = token.refresh_token
|
||||
|
||||
post_data = {"grant_type": "refresh_token",
|
||||
"refresh_token": refreshtoken,
|
||||
}
|
||||
|
||||
auth_string = f"{FAKTUROID_CLIENT_ID}:{FAKTUROID_CLIENT_SECRET}"
|
||||
|
||||
headers = {
|
||||
'User-Agent': f"{FAKTUROID_APP_NAME} ({FAKTUROID_EMAIL})",
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Basic %s' % base64.b64encode(
|
||||
bytes(auth_string, 'utf-8')).decode('utf-8'),
|
||||
}
|
||||
|
||||
response = requests.post(token_url, data=json.dumps(post_data), headers=headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
|
||||
try:
|
||||
t = FakturoidToken.objects.get(id=1)
|
||||
t.acces_token = result['access_token'],
|
||||
t.expires_in = result['expires_in']
|
||||
t.save()
|
||||
except FakturoidToken.DoesNotExist:
|
||||
t = FakturoidToken(
|
||||
access_token = result['access_token'],
|
||||
expires_in = result['expires_in'],
|
||||
)
|
||||
t.save()
|
||||
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
def get_token():
|
||||
try:
|
||||
token = FakturoidToken.objects.get(id=1)
|
||||
except FakturoidToken.DoesNotExist:
|
||||
return 0
|
||||
|
||||
if token.updated_at + datetime.timedelta(seconds=token.expires_in) < timezone.now():
|
||||
fakturoid_token_refresh()
|
||||
|
||||
token = FakturoidToken.objects.get(id=1)
|
||||
|
||||
return token.access_token
|
||||
|
||||
def get_contacts(rower):
|
||||
res = requests.get(contacts_url, auth=auth, headers=headers)
|
||||
url = contacts_search_url+'?query='+urllib.parse.quote(rower.user.email)
|
||||
token = get_token()
|
||||
if not token:
|
||||
return None
|
||||
|
||||
authorizationstring = str('Bearer ' + token)
|
||||
headers = {
|
||||
'Authorization': authorizationstring,
|
||||
'User-Agent': f"{FAKTUROID_APP_NAME} ({FAKTUROID_EMAIL})",
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
#url = contacts_search_url+'?query='+urllib.parse.quote(rower.user.email)
|
||||
url = contacts_search_url
|
||||
params = {
|
||||
'query':urllib.parse.quote(rower.user.email)
|
||||
}
|
||||
dologging('braintreewebhooks.log','Searching Contact url :'+str(url))
|
||||
|
||||
res = requests.get(url, auth=auth, headers=headers)
|
||||
print(url)
|
||||
print(headers)
|
||||
print(params)
|
||||
|
||||
res = requests.get(url, headers=headers, params=params)
|
||||
|
||||
print(res.status_code,res.text,res.reason, "aap")
|
||||
|
||||
dologging('braintreewebhooks.log','Searching Contact Status code '+str(res.status_code)+'\n')
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ def rower_process_fakturoidcallback(request):
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Basic %s' % base64.b64encode(
|
||||
bytes(auth_string, 'utf-8')).decode('utf-8'),
|
||||
}
|
||||
}
|
||||
|
||||
base_url = 'https://app.fakturoid.cz/api/v3/oauth/token'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user