2017-07-03 54 views
0

我有一個簡單的查詢API的Python腳本(使用requests庫),解析返回的JSON,並基於從API返回的數據構建重定向URL。該腳本作爲從本機運行的本地python腳本運行。Zapier代碼:Python Http

我的目標是讓用戶在其瀏覽器中打開一個URL,該URL會觸發python代碼運行,構建重定向URL並將用戶重定向到此新URL。

我可以使用Zapier Webhooks + Zapier Code來實現嗎?

Python的看起來像這樣(的細節差異):

# A constant for the API query 
query_str = "123" 

# A constant in the redirect URL 
resultant_constant = "93093" 

# Personal Access Tokenfor this service 
app_id = "the_app_id" 
secret = "the_secret_key" 

# Make API call and get a the JSON response 
resp = requests.get('https://api.example.com/path/to/endpoint/'+ query_str +'/endpoint?filter=theFilter&order=sort_date', auth=(app_id,secret)) 

# Gets the id of the object I want 
my_id = resp.json()['data'][0]['id'] 

report_url = "https://subdomain.example.com/path/"+resultant_constant+".ext?&object_id="+my_id 

return report_url 

回答

0

這是不可能有Zapier重定向瀏覽器 「GET」 請求另一個URL。 Zapier只接受發送給它的數據,並運行你在後臺設置的任何動作。

然而,減去重定向,仍然可以實現你最需要的東西:

使用「捕獲原始掛鉤」選項時出現的「GET」請求,當有人訪問由Zapier,IE提供的網址:https://hooks.zapier.com/hooks/catch/....

然後,「Code by Zapier」操作允許您運行Python代碼。