Jaseibert / QualtricsAPI

QualtricsAPI is a lightweight Python Package for the Qualtrics API.
https://www.qualtricsapi-pydocs.com
MIT License
18 stars 13 forks source link

Is there any way to authenticate without pool ID? #2

Closed YukunYangNPF closed 3 years ago

YukunYangNPF commented 3 years ago

Hello,

I just want to ask if the pool id is necessary for auth. The official API does not require that to get survey responses and my organization does not have an XM directory thus no pool ID.

Best,

Jaseibert commented 3 years ago

@YukunYangNPF so the pool id is known as the Directory ID and should only be required if you are attempting to access a method reserved for XM Directory users. It should not be required if you are trying to simply pull survey responses. Can you provide me some more information around when you receive the error?

YukunYangNPF commented 3 years ago

Yes. It's when I tried to add credentials.

from QualtricsAPI.Setup import Credentials

#Create an instance of Credentials
c = Credentials()

#Call the qualtrics_api_credentials() method
c.qualtrics_api_credentials(token='my token',data_center='iad1',directory_id=None)

So, it seems that directory is a required parameter in adding the credentials. If I l don't specify directory id or set it as None it raises an error.

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/QualtricsAPI/Setup/credentials.py in qualtrics_api_credentials(self, token, data_center, directory_id)
     22         '''
     23         assert len(token) == 40, 'Hey there! It looks like your api token is a the incorrect length. It needs to be 40 characters long. Please try again.'
---> 24         assert len(directory_id) == 20, 'Hey there! It looks like your api directory ID is a the incorrect length. It needs to be 20 characters long. Please try again.'
     25         assert directory_id[:5] == 'POOL_', 'Hey there! It looks like your directory ID is incorrect. You can find the directory ID on the Qualtrics site under your account settings. Please try again.'
     26 

TypeError: object of type 'NoneType' has no len()

And since I cannot add the credentials, I cannot pull survey responses.

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/QualtricsAPI/Survey/responses.py in get_responses(self, survey)
     51         :return: a Pandas DataFrame
     52         '''
---> 53         download_request = self.send_request(file_format='csv', survey=survey)
     54         with zipfile.ZipFile(io.BytesIO(download_request.content)) as survey_zip:
     55             for s in survey_zip.infolist():

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/QualtricsAPI/Survey/responses.py in send_request(self, file_format, survey)
     33         '''This method sends the request, and sets up the download request.'''
     34         file = None
---> 35         progress_id, url, headers = self.setup_request(file_format=file_format, survey=survey)
     36         check_progress = 0
     37         progress_status = "in progress"

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/QualtricsAPI/Survey/responses.py in setup_request(self, file_format, survey)
     20         assert survey[:3] == 'SV_', 'Hey there! It looks like your survey ID is incorrect. You can find the survey ID on the Qualtrics site under your account settings. Please try again.'
     21 
---> 22         headers, url = self.header_setup(content_type=True, xm=False, path='responseexports/')
     23         payload = '{"format":"' + file_format + '","surveyId":"' + survey +'"}'
     24         request = r.request("POST", url, data=payload, headers=headers)

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/QualtricsAPI/Setup/credentials.py in header_setup(self, content_type, xm, path)
     39         :return: a HTML header and base url.
     40         '''
---> 41         header = {"x-api-token": os.environ['token']}
     42         path = 'directories/{0}/'.format(os.environ['directory_id']) if xm else path
     43         base_url = f"https://{os.environ['data_center']}.qualtrics.com/API/v3/{path}"

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py in __getitem__(self, key)
    673         except KeyError:
    674             # raise KeyError with the original key value
--> 675             raise KeyError(key) from None
    676         return self.decodevalue(value)
    677 

KeyError: 'token'
Jaseibert commented 3 years ago

@YukunYangNPF Okay, Perfect, think I see the issue. Let me run a test or two to confirm the issue is fixed and I will push out an updated version.

Jaseibert commented 3 years ago

@YukunYangNPF I just implemented a fix to the issue, you will need to install the latest version of the package. Use pip install QualtricsAPI==0.4.3 or pip3 install QualtricsAPI==0.4.3 depending on your python installation.

Then to get the responses use:

#1. Import the dependencies
from QualtricsAPI.Setup import Credentials
from QualtricsAPI.Survey import Responses

#2. Set your credentials
Credentials().qualtrics_api_credentials(token='<Your API Token>',data_center='<Your Data Center>')

#3. Call get survey responses
df = Responses().get_responses(survey='<Survey ID>')
df.head()
YukunYangNPF commented 3 years ago

@YukunYangNPF I just implemented a fix to the issue, you will need to install the latest version of the package. Use pip install QualtricsAPI==0.4.3 or pip3 install QualtricsAPI==0.4.3 depending on your python installation.

Then to get the responses use:

#1. Import the dependencies
from QualtricsAPI.Setup import Credentials
from QualtricsAPI.Survey import Responses

#2. Set your credentials
Credentials().qualtrics_api_credentials(token='<Your API Token>',data_center='<Your Data Center>')

#3. Call get survey responses
df = Responses().get_responses(survey='<Survey ID>')
df.head()

Thanks. It seems to work.

Another question: how can I specify get the survey responses with labels or not.

For example, I want to have the choice to download the survey responses in labels ('Very good", 'Good') or in values (1,2,3..)

Jaseibert commented 3 years ago

@YukunYangNPF Excellent, I am glad it is working for you. To your other question, that functionality is currently not built into the package. By default, you will receive the values, instead of the labels. I can add this as a feature request and build in that functionality here in the near future.