SurveyMonkey API not filtering on status - api

I'm new to the SurveyMonkey API (and relatively new to Python in general).
I'm trying to retrieve the contacts that have either bounced or opted out (with 2 separate calls). This code was working last week (at that time, the query parameters were permissible on the contacts/bulk endpoint and they are now only on the contacts endpoint).
The code below is only returning active contacts (the default). It does 'accept' when I change the per_page parameter but I'm not sure why the status isn't working.
Where am I going wrong?
token = <our_token>
client = requests.session()
headers = {
"Authorization": "Bearer %s" % token,
"Content-Type": "application/json"
}
HOST = "https://api.surveymonkey.com"
EMAIL_ENDPOINT = "/v3/contacts"
OPTOUT = '?status=optout'
PER_PAGE = '&per_page=1000'
optout_url = "%s%s%s%s" % (HOST, EMAIL_ENDPOINT, OPTOUT, PER_PAGE)
optout_api = client.get(optout_url, headers=headers).json()

nm. Heard back from SurveyMonkey and it's an issue on their end.

Related

Karate - Trouble passing correct headers for authorization

I am have some problems passing in the correct headers for my graphql endpoints
The use case in Postman:
call requestToken endpoint to obtain sessionToken value
requestToken response contains Key Value " and Token Value.
For subsequent calls, I set postman headers as:
Key = X_SESSION_TOKEN Value = Token Value
The user case in Karate
1st feature 'requestToken.feature' successfully calls and stores key + tokenValue
2nd feature successfully defines and prints the token value
here is my 2nd request:
Feature: version
Background:
* url 'http://api-dev.markq.com:5000/'
* def myFeature = call read('requestToken.feature')
* def authToken = myFeature.sessionToken
* configure headers = { 'X_SESSION_TOKEN': authToken , 'Content-Type': 'application/json' }
Scenario: get version
Given path 'query'
Given text query =
"""
query {
version
}
"""
And request { query: '#(query)' }
When method POST
Then status 200
And print authToken
And print response
I am not sure I send the headers right. Its coming back 200, but I keep getting a error 'token malformed' in the response message
Any suggestions? New at this, thanks!
Honestly this is hard to answer, a LOT depends on the specific server.
EDIT: most likely it is this change needed, explained here: https://github.com/intuit/karate#embedded-expressions
* configure headers = { 'X_SESSION_TOKEN': '#(authToken)' , 'Content-Type': 'application/json' }
2 things from experience:
should it be X-SESSION-TOKEN
add an Accept: 'application/json' header
And try to hardcode the headers before attempting call etc.
Here is an example that works for me:
* url 'https://graphqlzero.almansi.me/api'
* text query =
"""
{
user(id: 1) {
posts {
data {
id
title
}
}
}
}
"""
* request { query: '#(query)' }
* method post
* status 200

Getting header with selenium-wire python

Need to get header from request with selenium wire. I'm taking cookie from header
result_cookies = ""
for request in self.browser.iter_requests():
result = request.headers.get("cookie")
if result:
if len(result_cookies) < len(result):
result_cookies = result
But I have there different cookies, where userCategory=PU, and userCategory=LIM, how i can get cookie where userCategory is LIM.
Part of cookie:
g2usersessionid=e792fb427388bfbfd2f6d8c82163d763; G2JSESSIONID=A27799E20A8A42058502875CD1617C61-n1; userLang=en; visid_incap_242093=ivG0wNBcTX2qbGGfef/Kzsp15WAAAAAAQUIPAAAAAAALk1ZNmpA0m0hbUNh68t/9; incap_ses_260_242093=g/XKTK5T3TznIlAEpLSbA8p15WAAAAAALxw6bB7fGHsf1fHHG2AGPg==; userCategory=LIM; copartTimezonePref=%7B%22displayStr%22%3A%22CEST%22%2C%22offset%22%3A2%2C%22dst%22%3Atrue%2C%22windowsTz%22%3A%22Europe%2FBerlin%22%7D; timezone=Europe%2FBerlin;
This how I make use of selenium-wire to get the cookie from the headers:
for request in self.driver.requests:
if request.response:
print(
request.url,
request.response.status_code,
request.response.headers['Content-Type'],
request.response.headers['set-cookie']
)
if "ivc_id" in request.response.headers['set-cookie']:
cookie=request.response.headers['set-cookie']
print(cookie)
Is this what you are looking for?

Pipe request, resend request to another endpoint (make a common endpoint that lead to another endpoint

I want to create a common /me endpoint for all my roles (operators, managers, simple users, etc.) for all 4 methods, GET, POST, DELETE, UPDATE.
I found 2 methods to do it:
Call correspond function inside endpoint (requires to create a separate endpoint for every HTTP method).
Redirection (Allow to create a single function).
That is how it looks:
#router.get(path="/me", status_code=200) # Var 1
async def get_my_profile(
token: str = Depends(config.oauth2_scheme),
postgres_session: AsyncSession = Depends(database.get_db), ):
try:
token_payload = jose_jwt.get_unverified_claims(token=token)
if token_payload['role'] == config.USER_ROLE:
return await routers.users.get_user(user_id=int(token_payload['sub']),
token=token,
postgres_session=postgres_session)
except Exception as error:
services.error_handler(error)
#router.api_router(path="/me", status_code=200, methods=["GET", "POST", "DELETE", "PATCH"]) # Var 2
async def get_my_profile(request: Request, token: str = Depends(config.oauth2_scheme),):
try:
token_payload = services.get_token_payload(token=token)
scheme_ip_port = f'{request.url.scheme}://{request.url.hostname}:{request.url.port}'
urls = [f"{scheme_ip_port}/users/{token_payload['sub']}",
f"{scheme_ip_port}/operators/{token_payload['sub']}",
f"{scheme_ip_port}/clinics/employees/{token_payload['sub']}",
]
for i, url in enumerate(iterable=urls):
if i == token_payload['role']:
return responses.RedirectResponse(url=url)
except Exception as error:
services.error_handler(error)
Which one is preferred to use? What drawbacks every solution has?

How to navigate and validate through all the pages of a api response

I have a scenario where the api returns payload response in pages if the payload has lot of data.
Request:
Background:
* url url
* call read('classpath:examples/common.feature')
And header accesstoken = accessToken
And header accept = '*/*'
And header Accept-Encoding = 'gzip, deflate, br'
Scenario: Get Scores
* param start = '2020-07-01'
Given path '/scores'
When method Get
Then status 200
* def totalPages = response.totalPages
* def response = {"requestId": "6a4287f35112",
"timestampMs": 1595228005245,
"totalMs": 51,
"page": 1,
"totalPages": 100,
"data": [.......]}
After this i am getting total pages, and need to navigate through all the pages by passing the same request with additional * param page = #page_number and validate response is 200. page_number has to be iterated from 2 to 100.
Thought of using Karate loop or calling feature file and building dynamic data and using dynamic data driven feature, but not sure how to proceed.
Please advise
I think the easiest option is to write a second feature file and call it in a loop.
* def totalPages = 10
* def pages = karate.repeat(totalPages, function(i){ return { page: i } })
* call read('second.feature') pages

Google task API authentication issue ruby

I am having the problem to authenticate a user for google tasks.
At first it authenticates the user and do things perfect. But in the second trip it throws an error.
Signet::AuthorizationError - Authorization failed. Server message:
{
"error" : "invalid_grant"
}:
following is the code:
def api_client code=""
#client ||= (begin
client = Google::APIClient.new
client.authorization.client_id = settings.credentials["client_id"]
client.authorization.client_secret = settings.credentials["client_secret"]
client.authorization.scope = settings.credentials["scope"]
client.authorization.access_token = "" #settings.credentials["access_token"]
client.authorization.redirect_uri = to('/callbackfunction')
client.authorization.code = code
client
end)
end
get '/callbackfunction' do
code = params[:code]
c = api_client code
c.authorization.fetch_access_token!
result = c.execute("tasks.tasklists.list",{"UserId"=>"me"})
unless result.response.status == 401
p "#{JSON.parse(result.body)}"
else
redirect ("/oauth2authorize")
end
end
get '/oauth2authorize' do
redirect api_client.authorization.authorization_uri.to_s, 303
end
What is the problem in performing the second request?
UPDATE:
This is the link and parameters to user consent.
https://accounts.google.com/o/oauth2/auth?
access_type=offline&
approval_prompt=force&
client_id=somevalue&
redirect_uri=http://localhost:4567/oauth2callback&
response_type=code&
scope=https://www.googleapis.com/auth/tasks
The problem is fixed.
Solution:
In the callbackfunction the tokens which are received through the code provided by the user consent are stored in the database.
Then in other functions just retrieve those tokens from the database and use to process whatever you want against the google task API.
get '/callbackfunction' do
code = params[:code]
c = api_client code
c.authorization.fetch_access_token!
# store the tokens in the database.
end
get '/tasklists' do
# Retrieve the codes from the database and create a client
result = client.execute("tasks.tasklists.list",{"UserId"=>"me"})
unless result.response.status == 401
p "#{JSON.parse(result.body)}"
else
redirect "/oauth2authorize"
end
end
I am using rails, and i store the token only inside DB.
then using a script i am setting up new client before calling execute, following is the code.
client = Google::APIClient.new(:application_name => 'my-app', :application_version => '1.0')
client.authorization.scope = 'https://www.googleapis.com/auth/analytics.readonly'
client.authorization.client_id = Settings.ga.app_key
client.authorization.client_secret = Settings.ga.app_secret
client.authorization.access_token = auth.token
client.authorization.refresh_token = true
client.authorization.update_token!({access_token: auth.token})
client.authorization.fetch_access_token!
if client.authorization.refresh_token && client.authorization.expired?
client.authorization.fetch_access_token!
end
puts "Getting accounts list..."
result = client.execute(:api_method => analytics.management.accounts.list)
puts " ===========> #{result.inspect}"
items = JSON.parse(result.response.body)['items']
But,it gives same error you are facing,
/signet-0.4.5/lib/signet/oauth_2/client.rb:875:in `fetch_access_token': Authorization failed. Server message: (Signet::AuthorizationError)
{
"error" : "invalid_grant"
}
from /signet-0.4.5/lib/signet/oauth_2/client.rb:888:in `fetch_access_token!'
Please suggest why it is not able to use the given token? I have used oauth2, so user is already authorized. Now i want to access the api and fetch the data...
===================UPDATE ===================
Ok, two issues were there,
Permission is to be added to devise.rb,
config.omniauth :google_oauth2, Settings.ga.app_key,Settings.ga.app_secret,{
access_type: "offline",
approval_prompt: "" ,
:scope => "userinfo.email, userinfo.profile, plus.me, analytics.readonly"
}
refresh_token must be passed to the API call, otherwise its not able to authorize.
I hope this helps to somebody, facing similar issue.