Event Notification Delivery Method - Ebay error - HTTP status code 405 - python

I have used the following guide - https://developer.ebay.com/marketplace-account-deletion
So I've created a flask based website on python and uploaded in Heroku,
https://ebay-deletion.herokuapp.com/
If you create a query parameter, for the challenge code like so:
https://ebay-deletion.herokuapp.com/?challenge_code=123
It returns a challenge response and doc type (JSON).
I don't understand but why but I get this error below:
Notification delivery failed with HTTP status code 405 from https://ebay-deletion.herokuapp.com. Please ensure that the marketplace account deletion notification endpoint is ready to receive notifications.
Any thoughts on how to solve this?
Ebay's help page is just so terrible. Their python code examples for hashing in the top link have semi-colons after each line! It's close to completely useless.
Here is the Python Flask code:
#app.route('/')
def index():
args = request.args
args_dict = args.to_dict()
try:
resp_hash = hashlib.sha256(
args_dict['challenge_code'].encode() + verification_token.encode() + endpoint.encode())
resp = {'challengeResponse': resp_hash.hexdigest()}
return jsonify(resp), 200, {'content-type': 'application/json'}
except KeyError:
err = {'status_code': 400, 'message': 'no challenge response in params'}
return jsonify(err), 400, {'content-type': 'application/json'}

Related

Flask - GET request depending on POST result to update template

I am developping a python flask app to launch databricks jobs (executing IPython notebooks) without using databricks website.
I have a post request lauching the execution of a notebook.
#app.route('/', methods=['GET', 'POST'])
def main():
response = requests.get(
url=url1,
json=data,
headers={'Authorization': 'Bearer %s' % TOKEN }
)
notebookDict[item] = json.loads(response.text)
# [... some logic to sort notebook dict ...]
for item in listeNomNotebook:
listeNomNotebook[item].sort()
# my post method for executing notebook through api
if request.method == 'POST':
#[... some variable ...]
params = {
'run_name' : f'{notebook_name}',
'existing_cluster_id' : cluster_id,
'notebook_task' : {
'notebook_path' : f'{pathstr}',
'base_parameters' : parameters
},
}
response = requests.post(
url=url2,
json=params,
headers={'Authorization': 'Bearer %s' % TOKEN }
)
return render_template('index.html', data=listeNomNotebook)
What i want is, from the response of this POST request, which is a job_id, to execute a GET request with this job_id as a parameters to know the result of the execution, failure or success.
I want then to update the website dinamically to show the lifecycle of the job. By this I mean having my GET request in a loop until the state from the response shows terminated. Here is my test code to do this :
job_id = response.json()
print(job_id)
state = ""
while True:
result = requests.get(
url= url3,
json=job_id,
headers={'Authorization': 'Bearer %s' % TOKEN }
)
dict_result = result.json()
life_cycle = dict_result["state"]["life_cycle_state"]
state = dict_result["state"]["state_message"]
print(life_cycle)
if life_cycle == "TERMINATED":
state = dict_result["state"]["result_state"]
print(state)
break
sleep(10)
I tryed to use ajax but the CORS policy wouldn't let me use that solution (even with an add on on my browser and using flask-cors).
How can i show the result of my notebook on my index page dynamically ? Showing the lifecycle : pending, running , terminated.
And then showing the result : Success or Failure.
All of this is part of the response from the GET request of the databricks api :
api/2.0/jobs/runs/get
I suggest you do it in this order:
Post to / to start the request, get the job_id in the frontend.
In the front-end, periodically query for job status.
In case the problem is with CORS, then please open a question concerning that after verifying that existing answers don't work. There could be many reasons why you get CORS errors, be sure to state the actual blocker.

Get request with same tooken return results in Postman but unauthorized in C# code

I am posting a GET request to this URL with a bearer token:
http://api.viagogo.net/catalog/events?page_size=1000&sort=resource_version&min_resource_version=75939827358
It returns results in Postman as expected.
When i call the same api with same token in code (C#), i get unauthorized 401 response.
This is my code:
var eventsUri = new Uri(eventsUrl);
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
httpClient.DefaultRequestHeaders.Add("User-Agent", "App Name");
var eventResponseFromHttp = await httpClient.GetAsync(eventsUri);
var responseContent = await eventResponseFromHttp.Content.ReadAsStringAsync();
var eventsFromHttps = JsonConvert.DeserializeObject<EventResponse>(responseContent);
But the weird thing is this:
If i call the same api without sort and min_resource_version query string parameters from code, it returns the results with 200:
http://api.viagogo.net/catalog/events?page_size=1000
I have no idea whats going on here. This is happening in a .Net Framework 4.7 application.
Any help is appreciated. Thanks.
UPDATE:
The same url with same token returns 200 and results using Python.

line bot - how to get started

i've just started working with line-bot and followed the tutorial here: https://developers.line.biz/en/docs/messaging-api/building-bot/
However, I still don't understand how I can connect with my line app account, to send messages, and have these messages appear back in python.
The below is the script I copied from line tutorial.
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage
app = Flask(__name__)
line_bot_api = LineBotApi('foo', timeout=20)
handler = WebhookHandler('bar')
user_profile = 'far'
#app.route("/", methods=['GET'])
def home():
profile = line_bot_api.get_profile(user_profile)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url)
print(profile.status_message)
return '<div><h1>ok</h1></div>'
#app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
#handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='hello world'))
if __name__ == "__main__":
app.run(debug=True)
What am I missing, or how can I connect with the line app to send and receive messages?
I followed that tutorial and was able to successfully create a bot that just echoes messages in uppercase:
Your question is how to "connect" your bot's code with the LINE app. The three most important parts of the tutorial are probably:
Adding the bot as a friend, you do this by scanning its QR code with the LINE app
When you create a channel for your bot, you need to enable "Webhooks" and provide an https endpoint which is where LINE will send the interaction events your bot receives. To keep this simple for the purposes of this answer, I created an AWS Lambda function and exposed it via API Gateway as an endpoint that looked something like:
https://asdfasdf.execute-api.us-east-1.amazonaws.com/default/MyLineBotFunction. This is what I entered as the Webhook URL for my bot.
Once you are successfully receiving message events, responding simply requires posting to the LINE API with the unique replyToken that came with the message.
Here is the Lambda function code for my simple yell-back-in-caps bot:
import json
from botocore.vendored import requests
def lambda_handler(event, context):
if 'body' in event:
message_event = json.loads(event['body'])['events'][0]
reply_token = message_event['replyToken']
message_text = message_event['message']['text']
requests.post('https://api.line.me/v2/bot/message/reply',
data=json.dumps({
'replyToken': reply_token,
'messages': [{'type': 'text', 'text': message_text.upper()}]
}),
headers={
# TODO: Put your channel access token in the Authorization header
'Authorization': 'Bearer YOUR_CHANNEL_ACCESS_TOKEN_HERE',
'Content-Type': 'application/json'
}
)
return {
'statusCode': 200
}

Telegram bot webhook and amazon lambda

I've tried to create my first telegram bot hosting the code as an amazon lambda instance, i suppose i should return something to the webhook 'cause it keep saying "Wrong response from the webhook: 502 Bad Gateway".
Here is part of my code:
def msgSend(text, chat_id):
url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
response = requests.get(url)
content = response.content.decode("utf8")
return content
def handle(msg):
sender = msg['from']['username']
id_gruppo = msg['chat']['id']
if sender == NAME:
testo = msg['text']
usernames = [x.replace('#','') for x in rx.findall(text)]
map(foo, usernames)
msgSend(confirm_mess, id_group)
return
def main(event, context):
response = ast.literal_eval(event['body'])
handle(response['message'])
return {
'something': 'something'
}
Actually the process works fine enough, the messages are received by my lambda and everything works like a charm, except for one thing, the confirmation message is sent over and over endlessly and the webhooks never marks the messages as read.
here is the response of getWebHookInfo:
{"ok":true,"result":{"url":"https://t2rt9guj3h.execute-api.us-west-2.amazonaws.com/prod/instabot","has_custom_certificate":false,"pending_update_count":19,"last_error_date":1489331750,"last_error_message":"Wrong response from the webhook: 502 Bad Gateway","max_connections":40}}
According to the bot helper the wh requires a 2XX code response...
Any idea about that?
According to the bot helper the wh requires a 2XX code response...
This is true. Your last statement should be
return {
statusCode: 200
}
If you don't return a successful response code, Telegram won't know what to do with it, which is why you see the HTTP 502 Bad Gateway. I was hitting this for awhile also :)

Paypal Payflow Pro return url does not work correctly - 'processTransaction.do' appended - raises 404

currently I am trying to migrate a working php paypal payflow implementation to a new python-based system.
I use secure token together with hosted checkout pages. The secure token works fine and I get redirected to the checkout page as well (although it has horrible formatting errors).
THE PROBLEM: after the payment it should redirect to the return url. This works BUT 'processTransaction.do' is appended to it. So my return url is defined as:
'https://mywebsite.com/paypal/succes/'
but i get redirected to
'https://mywebsite.com/paypal/succes/processTransaction.do'
and this raises a 404.
My secure token request parameters:
params = {}
params["PARTNER"] = "paypal"
params["VENDOR"] = "...."
params["TRXTYPE"] = "S"
params["AMT"] = payment_amount #amount to pay
params["CREATESECURETOKEN"] = "Y"
params["SECURETOKENID"] = time.time() #needs to be unique
params["USER"] = "...."
params["PWD"] = "...."
Then I send the request and catch the return which looks like this:
RESULT=0&SECURETOKEN=QQQc0rQZ8TkKSNMqU3Mg2og7o
SECURETOKENID=1431563231.24&RESPMSG=Approved
Afterwards I send the request for the checkout page with the following paramters:
params["SECURETOKEN"] = securetoken
params["SECURETOKENID"] = securetokenid
to: https://payflowpro.paypal.com
I use this code to send the requests:
data = urllib.urlencode(params)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
response_text = response.read()
The return url is set in the paypal manager with return type as POST and "Show Confirmation Page" is set to "On my website".
Does somebody know what is wrong and how to fix it?
Thanks!

Categories

Resources