petermcd / monzo-api

Python package to interact with the API provided by Monzo bank
MIT License
29 stars 5 forks source link

403 error when trying to list transactions #20

Closed psleep closed 2 years ago

psleep commented 2 years ago

Hi @petermcd, This is probably me misunderstanding something but I am testing your API with retrieving transactions (I am working on something where I want to retrieve the description of the last transaction) and I receive a 403 error whenever I try to do anything with transactions.

If I have read your API correctly (and borrowed some code from your viewer), the following code should return a list of all transactions.

`monzo = Authentication( client_id=client_id, client_secret=client_secret, redirect_url=redirect_uri, access_token=access_token, access_token_expiry=expiry, refresh_token=refresh_token )

Instantiate our handler

handler = FileSystem('/home/phil/Documents/monzo-token')

Register the handler

monzo.register_callback_handler(handler)

balance = Account.fetch(monzo)[0].balance.balance accountid = Account.fetch(monzo)[0].account_id since = None print((balance) / 100) print(accountid)

transactions = Transaction.fetch(auth=monzo, account_id=accountid, since=since) print(transactions)`

The balance and accountid are returned without any issues but transactions is giving me a 403 forbidden error. Do you have any pointers at all or is this a Monzo issue?

petermcd commented 2 years ago

I can give a fuller response this evening but I believe this is because the credentials were created greater than 5 minutes ago. For transactions you can only fetch transactions greater than 90 days ago within the first 5 minutes.

As you are assigning None to since it is trying to fetch all transactions. To resolve this you can specify the since as a date time object with a time delta within the last 90 days.

If you take a look at monzo_data.py one of the methods has code that generates such a date going back 7 days

psleep commented 2 years ago

@petermcd perfect that solved it! I was trying to simplify my code to figure out where it was having an issue. Using 7 days, the transactions are now returned. Thanks for your help again!

petermcd commented 2 years ago

Not a problem. Glad it helped. Be sure to let me know if you come across any issues or have any suggestions.

SomeDevWeb commented 1 year ago

Hi guys. I'm trying to do the same thing - retrieving transactions and this post has been helpful for getting over the first hurdles, but I'm now stuck on the RFC3339 format. I get the error:

  File "C:\Users\User\monzo-api\examples\hosting_automanage.py", line 46, in <module>
    transactions = Transaction.fetch(auth=monzo, account_id=accountid, since=since)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Roaming\Python\Python311\site-packages\monzo\endpoints\transaction.py", line 543, in fetch
    data['since'] = format_date(since)
                    ^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Roaming\Python\Python311\site-packages\monzo\helpers.py", line 47, in format_date
    return date.strftime('%Y-%m-%dT%H:%M:%SZ')
           ^^^^^^^^^^^^^

I noticed @petermcd you mentioned a monzo_data.py, but i couldn't find that anywhere. Could you please clarify where that is please? What I need is to get the transactions from the beginning of the current month so I can search for certain ones. So here is my code if it helps:

    currentMonth = datetime.now().month
    date_time_str = '2023-'+str(currentMonth)+'-1 00:00:01' #start from the first day of the current month
    date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
    dateRFC3339 = date_time_obj.isoformat("T") + "Z"
    print ("Date in RFC3339 format", dateRFC3339)
    since = dateRFC3339
    transactions = Transaction.fetch(auth=monzo, account_id=accountid, since=since)
    print(transactions)

Thanks a lot for any help!

petermcd commented 1 year ago

Hi @SomeDevWeb

monzo_data.py was taken out of the code a while ago (it was a tool I was working on that allowed people to query the API without needing to code). The source code can be found in the git history. The following is the link to the last version contained in this repo:

https://github.com/petermcd/monzo-api/blob/v0.1.6/monzo/viewer/monzo_data.py

The part that should help you would be

today = datetime.date.today()
since_date = today - datetime.timedelta(days=7)
since = datetime.datetime(
    year=since_date.year,
    month=since_date.month,
    day=1,
)

since can then be passed into fetch transactions as is. I have slightly modified the code, instead of using since_date.day I have hard coded 1 (cant remember off the top of my head if it should be 0 or 1 in this instance sometimes days and months are zero based).

Please note that I am using timedelta to go back 7 days here you of course may not want to do this.

SomeDevWeb commented 1 year ago

WOW, amazing! It actually works!!! :) Thanks a lot! But as a thought, when you do print (since) you get 2023-06-01 00:00:00 So does that mean the monzo doc is wrong??? In there it says: since Optional | Start time as RFC3339 encoded timestamp (2009-11-10T23:00:00Z) https://docs.monzo.com/#list-transactions

Anyway, it's great it works the way you suggested! You're a legend!