Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Querying Error#2 and Paginating with Offset #18

Open
ViolaHempel opened this issue May 16, 2016 · 13 comments
Open

Querying Error#2 and Paginating with Offset #18

ViolaHempel opened this issue May 16, 2016 · 13 comments

Comments

@ViolaHempel
Copy link

ViolaHempel commented May 16, 2016

Hi Josh,

There is not a lot of documentation/tutorials on Pardot API, so thanks a lot for creating PyPardot!

import requests
import pypardot
import json
import pandas as pd
from pypardot.client import PardotAPI

p = PardotAPI(
                email='my_email',
                password='my_pwd',
                user_key='my_API'
                )

p.authenticate()

prospects = p.prospects.query(created_after='yesterday')
total = prospects['total_results'] # total number of matching records
for prospect in prospects['prospect']:
  print(prospect.get('first_name'))`

`Traceback (most recent call last):
  File "C:\Users\viola\Documents\Pardot\Pardot_API.py", line 18, in <module>
    prospects = p.prospects.query(created_after='yesterday')
  File "C:\Python27\lib\site-packages\pypardot\objects\prospects.py", line 15, in query
    result = self._get(path='/do/query', params=kwargs)
  File "C:\Python27\lib\site-packages\pypardot\objects\prospects.py", line 161, in _get
    result = self.client.get(object=object, path=path, params=params)
  File "C:\Python27\lib\site-packages\pypardot\client.py", line 80, in get
    raise err
pypardot.errors.PardotAPIError: Error #2: Invalid action

What could went wrong in prospects.py?

  • How could I fetch all visitoractivities in 2016, for example? You mention to use offset, but how would I make it work in a loop, so that I don't have to specify the offset parameter in the url all the time?

I wrote a while loop and used request/json libraries to get downloads data via Zendesk API call, but Zendesk response format has "next_page" attribute that makes it easy:

import pandas as pd
import requests
import json

downloads_DF = []
url = 'https://my_website.com/reports/api/v1/download-history/?start_date=2016-03-18&end_date=2016-04-30&ordering=timestamp'

while url:
    response = requests.get(url, headers={'Authorization': 'Token hex_number'})
    if response.status_code != 200:
        print('Error with status code {}'.format(response.status_code))
        exit()
    data = response.json()
    downloads_DF.extend(data['results'])
    **url = data['next']**

downloads_data = {'results': downloads_DF}
#Creating Pandas dataframe
recent_downloads_df = pd.DataFrame(downloads_data['results'], columns = ['name', 'timestamp', 'filename', 'platform', 'token', 'version', 'user', 'type', 'description' ])

Is there a way to do something similar with PyPardot?

Thanks!
Viola

@youngtechc
Copy link

youngtechc commented May 17, 2016

Hi Viola,

I cannot replicate with my instance. The error you're getting pypardot.errors.PardotAPIError: Error #2: Invalid action is coming directly from Pardot and not an issue with the package. Are you sure there isn't an issue with your access or ability to perform a get?

Can you try running the query in an http client like cURL or Postman?

  1. First get an access token. curl -k -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "email=<email>&password=<password>&user_key=<user_key>" "https://pi.pardot.com/api/login/version/3". Returns an API Key.
  2. With API Key in hand, call the query:
    curl -k -X GET "https://pi.pardot.com/api/prospect/version/3/do/query?user_key=<user_key>&api_key=<api_key>&format=json&created_after=yesterday"

@ViolaHempel
Copy link
Author

ViolaHempel commented May 17, 2016

Hi Rob,

Thanks for the suggestion. Yes, I did try it in Pardot API Console first, it returned my API Key and the query run successfully.

I just followed your steps in cURL (7.48.0 version from [here](curl 7.48.0 - 64-bit) ), but it gave me the error:

curl: no URL specified!  
curl: try 'curl --help' or 'curl --manual' for more information    
'password' is not recognized as an internal or external command,   operable program or batch file.
'user_key' is not recognized as an internal or external command,   operable program or batch file.  

I double checked on the typos, but I couldn't find any.

I run the PyPardot script again and got the same error as yesterday. What else could be wrong?

Thanks!
Viola

@youngtechc
Copy link

what's your operating system?

@ViolaHempel
Copy link
Author

Windows 7 Professional, 64-bit

@youngtechc
Copy link

youngtechc commented May 17, 2016

Ah. My statements were for Linux. Try adjusting for Windows. I had to down load the "ssh support" version and run it in insecure mode (-k). Try the commands above again. Replacing the <variable> with your own values.

@ViolaHempel
Copy link
Author

Thanks, the curl command worked! I figured it must be a syntax problem :)

The script from the manual though is still throwing me Error#2. If you copy paste it and insert your values, does it execute on your machine?

import requests
import pypardot
import json
import pandas as pd
from pypardot.client import PardotAPI

p = PardotAPI(
                email='my_email',
                password='my_pwd',
                user_key='my_user_key'
                )

p.authenticate()

prospects = p.prospects.query(created_after='yesterday')
total = prospects['total_results'] # total number of matching records
for prospect in prospects['prospect']:
  print(prospect.get('first_name'))

If I try to define api_key argument under p variable below, it doesn't work too.

@youngtechc
Copy link

I do not get the same error. The error code you're getting is a response from Pardot. When you ran the curl commands did the second one respond with the same error?

I have seen Pardot respond with inconsistent responses some times. For instance, if you attempt to send an email to someone who has unsubscribed you get a "#1 Prospect not found" error. We had to code around that.

@ViolaHempel
Copy link
Author

No, the curl commands for querying objects, as well as querying data via requests works perfectly. However, once I switch to PyPardot it respond with the error.

I just asked a friend to try PyPardot script with my credentials on his laptop (he uses Python 3.0) and he received the same error...

On a different note, what are the advantages of PyPardot? Essentially, it simplifies user's interaction with an API, because it handles expired API keys and 200 limits. Is my understanding correct?

Thanks for your help Rob!

@youngtechc
Copy link

I wish I could help more but it appears to be account related. You may want to contact Parrot to inquire. And yes, I find the PyPardot a huge help interacting with Pardot so I didn't have to create the libraries myself.

On May 18, 2016, at 6:51 PM, ViolaRudenko notifications@github.com wrote:

No, the curl commands for querying objects, as well as querying data via requests works perfectly. However, once I switch to PyPardot it respond with the error.

I just asked a friend to try PyPardot script with my credentials on his laptop (he uses Python 3.0) and he received the same error...

On a different note, what are the advantages of PyPardot? Essentially, it simplifies user's interaction with an API, because it handles expired API keys and 200 limits. Is my understanding correct?

Thanks for your help Rob!


You are receiving this because you commented.
Reply to this email directly or view it on GitHub

@ViolaHempel
Copy link
Author

ViolaHempel commented May 19, 2016

Thank you Rob! I contacted Pardot support. The interesting thing is that I still get the same error even if I use another person's admin credentials... The error traces to PyPardot itself, so the issue must be there I guess.

Traceback (most recent call last):
  File "C:\Users\viola\Documents\Lead Scoring\test.py", line 16, in <module>
    prospects = p.prospects.query(created_after='yesterday')
  File "C:\Python27\lib\site-packages\pypardot\objects\prospects.py", line 15, in query
    result = self._get(path='/do/query', params=kwargs)
  File "C:\Python27\lib\site-packages\pypardot\objects\prospects.py", line 161, in _get
    result = self.client.get(object=object, path=path, params=params)
  File "C:\Python27\lib\site-packages\pypardot\client.py", line 80, in get
    raise err
pypardot.errors.PardotAPIError: Error #2: Invalid action

Any thoughts @joshgeller?

@youngtechc
Copy link

I believe I owe you a huuuge apology. I am using the latest version from git. I was able to recreate your issue in PyPardot doing a pip install. Do me a favor and try:

pip uninstall pypardot
pip install -e git+https://github.com/joshgeller/PyPardot.git#egg=PyPardot

@ViolaHempel
Copy link
Author

Yay it worked!! Thank you so much @barcodez !!

I will now work on figuring out how to dynamically loop through all results with offset parameter! I've got millions of rows of data in visitoractivities.

@bbjunaid
Copy link

bbjunaid commented Aug 4, 2016

@barcodez I struggled with the same issue and had to modify the source code in the downloaded package, only to realize that the github source actually has the proper fix.

Can you update the latest package on pypi servers so others don't struggle with this in the future?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants