无法使用Python发送删除请求。

huangapple go评论100阅读模式
英文:

Unable to send delete request using python

问题

我正在尝试使用Python触发DELETE API来删除记录。

我登录,使用Python输入MFA代码,然后提交删除请求。在登录后,我提取了JWT_TOKEN并将其放入删除请求中。以下是我的登录、MFA验证和删除请求的代码。

登录:

  1. import requests,hmac,base64,struct,hashlib,time,mysql.connector
  2. import win32com.client
  3. import paramiko
  4. import pyotp
  5. import time
  6. api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
  7. todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
  8. response = requests.post(api_url, json=todo)
  9. token = response.json().get('jwt_token').get('token')
  10. time.sleep(45)

MFA代码:

  1. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  2. inbox = outlook.GetDefaultFolder(6) # "6"是文件夹的索引 - 在这种情况下是收件箱。您可以更改该数字来引用任何其他文件夹
  3. messages = inbox.Items
  4. message = messages.GetLast()
  5. body_content = message.body
  6. print(body_content)
  7. sub1="Console."
  8. sub2="This"
  9. idx1 = body_content.index(sub1)
  10. idx2 = body_content.index(sub2)
  11. res = ''
  12. # 获取两个子字符串之间的内容
  13. for idx in range(idx1 + len(sub1) + 1, idx2):
  14. res = res + body_content[idx]
  15. # 打印结果
  16. print("提取的字符串:" + res.strip())
  17. mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"
  18. todo = {"code": res.strip(), "email": "pchaubal+21022023SP@cytrio.com", "jwt_token": token}
  19. print(todo)
  20. headr = ""
  21. print(headr)
  22. response = requests.post(mfa_verify_url, json=todo, headers={'Authorization': 'Token ' + token})

删除请求:

  1. for x in range(1):
  2. x = 2663
  3. session = requests.session()
  4. session.trust_env = True
  5. tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/" + str(x)
  6. headers = {'Authorization': f'Token {token}'}
  7. response = requests.delete(tenant_delete_url, headers=headers)
  8. 即使在删除请求中提供了标头我仍然收到User not logged in错误请告诉我我在这里做错了什么

请注意,您的代码中可能有其他问题导致出现“User not logged in”错误,这些问题可能与JWT_TOKEN或API端点的权限相关。您需要仔细检查这些方面,以确定问题的根本原因。

英文:

I am trying to delete a record by triggering a DELETE API using python

I login, enter the MFA code using python and then submit the delete request. I have extracted the JWT_TOKEN after login and placing it in the delete request. Following is my code for the Login, MFA verification and Delete request

Login

  1. import requests,hmac,base64,struct,hashlib,time,mysql.connector
  2. import win32com.client
  3. import paramiko
  4. import pyotp
  5. import time
  6. api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
  7. todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
  8. response = requests.post(api_url, json=todo)
  9. token = response.json().get('jwt_token').get('token')
  10. time.sleep(45)

MFA Code

  1. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  2. inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this
  3. case,
  4. # the inbox. You can change that number to reference
  5. # any other folder
  6. messages = inbox.Items
  7. message = messages.GetLast()
  8. body_content = message.body
  9. print (body_content)
  10. sub1="Console."
  11. sub2="This"
  12. idx1 = body_content.index(sub1)
  13. idx2 = body_content.index(sub2)
  14. res = ''
  15. # getting elements in between
  16. for idx in range(idx1 + len(sub1) + 1, idx2):
  17. res = res + body_content[idx]
  18. # printing result
  19. print("The extracted string : " + res.strip())
  20. mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"
  21. todo = {"code":res.strip(), "email":"pchaubal+21022023SP@cytrio.com", "jwt_token":token}
  22. print(todo)
  23. headr = ""
  24. print (headr)
  25. response = requests.post(mfa_verify_url, json=todo, headers={'Authorization': 'Token
  26. '+token})

Delete request

  1. for x in range (1):
  2. x=2663
  3. session=requests.session()
  4. session.trust_env = True
  5. tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/"+str(x)
  6. #headers = "{'Authorization': '" + token + "'}"
  7. headers = {'Authorization': f'Token {token}'}
  8. #print('Authorization '+headers)
  9. #response = requests.delete(tenant_delete_url, headers={'Authorization ':
  10. f'{token}'})
  11. response = requests.delete(tenant_delete_url, headers=headers)

Even after suppling the header in th delete request, I get User not logged in.

Please let me know what I am doing wrong here

答案1

得分: 0

我通过建立一个会话并使用该会话执行 API 调用来解决了这个问题。以下是可运行的代码

  1. 导入请求、hmacbase64structhashlibtimemysql.connector
  2. 导入win32com.client
  3. 导入时间
  4. api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
  5. todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
  6. session = requests.session()
  7. response = session.post(api_url, json=todo)
  8. token = response.json().get('jwt_token').get('token')
  9. time.sleep(45)
  10. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  11. inbox = outlook.GetDefaultFolder(6) # "6" 指的是一个文件夹的索引 - 在这种情况下,
  12. # 收件箱。您可以更改该数字以引用
  13. # 任何其他文件夹
  14. messages = inbox.Items
  15. message = messages.GetLast()
  16. body_content = message.body
  17. print (body_content)
  18. sub1="Console."
  19. sub2="This"
  20. idx1 = body_content.index(sub1)
  21. idx2 = body_content.index(sub2)
  22. res = ''
  23. # 获取两者之间的元素
  24. for idx in range(idx1 + len(sub1) + 1, idx2):
  25. res = res + body_content[idx]
  26. # 打印结果
  27. mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"
  28. todo = {"code":res.strip(), "email":"pchaubal+21022023SP@cytrio.com", "jwt_token":token}
  29. session.headers={'Authorization': 'Token '+token}
  30. response = session.post(mfa_verify_url, json=todo)
  31. print(response.json())
  32. for x in range (1):
  33. x=13
  34. tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/"+str(x)
  35. headers = {'Authorization': f'Token {token}'}
  36. response = session.delete(tenant_delete_url, headers=headers)
  37. print(response.json())
英文:

I was able to resolve this issue by establishing a session and using the session to execute the API calls. Working code is given below

  1. import requests,hmac,base64,struct,hashlib,time,mysql.connector
  2. import win32com.client
  3. import time
  4. api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
  5. todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
  6. session = requests.session()
  7. response = session.post(api_url, json=todo)
  8. token = response.json().get('jwt_token').get('token')
  9. time.sleep(45)
  10. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  11. inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
  12. # the inbox. You can change that number to reference
  13. # any other folder
  14. messages = inbox.Items
  15. message = messages.GetLast()
  16. body_content = message.body
  17. print (body_content)
  18. sub1="Console."
  19. sub2="This"
  20. idx1 = body_content.index(sub1)
  21. idx2 = body_content.index(sub2)
  22. res = ''
  23. # getting elements in between
  24. for idx in range(idx1 + len(sub1) + 1, idx2):
  25. res = res + body_content[idx]
  26. # printing result
  27. mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"
  28. todo = {"code":res.strip(), "email":"pchaubal+21022023SP@cytrio.com", "jwt_token":token}
  29. session.headers={'Authorization': 'Token '+token}
  30. response = session.post(mfa_verify_url, json=todo)
  31. print(response.json())
  32. for x in range (1):
  33. x=13
  34. tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/"+str(x)
  35. headers = {'Authorization': f'Token {token}'}
  36. response = session.delete(tenant_delete_url, headers=headers)
  37. print(response.json())

huangapple
  • 本文由 发表于 2023年5月6日 16:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定