B4J Question Call PayPal Api to get transactions .... ChatGPT can't do it.

RWK

Member
Licensed User
Hi there,

i ask ChatCPT to write a function to log into PayPal Account get the transactions and write them to a file. It should use a fakeApi Key for the example.....


ChatGPT solution:
Sub LoginToPaypalAndGetTransactions(apiKey As String)
    'Create a new HttpRequest
    Dim request As HttpRequest
    request.Initialize

    'Set the endpoint URL for login
    Dim loginUrl As String = "https://api.paypal.com/v1/oauth2/token"

    'Set the request headers
    request.SetRequestHeader("Accept", "application/json")
    request.SetRequestHeader("Accept-Language", "en_US")
    request.SetRequestHeader("Authorization", "Basic " & apiKey)

    'Set the request parameters
    Dim params As Map
    params.Initialize
    params.Put("grant_type", "client_credentials")

    'Send the request
    Dim response As HttpResponse = request.Post2(loginUrl, params)

    'Check if login was successful
    If response.Success Then
        'Parse the JSON response text
        Dim parser As JSONParser
        parser.Initialize(response.GetString("UTF8"))
        Dim root As Map = parser.NextObject

        'Save the access token
        Dim accessToken As String = root.Get("access_token")

        'Use the access token to retrieve the account transactions
        Dim transactionsUrl As String = "https://api.paypal.com/v1/reporting/transactions"
        request.SetRequestHeader("Authorization", "Bearer " & accessToken)
        response = request.Get(transactionsUrl)

        'Check if retrieval of account transactions was successful
        If response.Success Then
            'Parse the JSON response text
            parser.Initialize(response.GetString("UTF8"))
            root = parser.NextObject

            'Save the account transactions in a variable
            Dim transactions As List = root.Get("transactions")

            'Write the account transactions to a text file
            File.WriteString(File.DirInternal, "transactions.txt", transactions.ToString)
            Log("Account transactions successfully saved.")
        Else
            Log("Error retrieving account transactions: " & response.ErrorMessage)
        End If
    Else
        Log("Error logging in: " & response.ErrorMessage)
    End If
End Sub

This will not work. In fact the used Methods seems to be already not correct.
Maybe the comunity is much better then the IP and can give me an example to do this task?

Greetings
 
Top