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

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

Unable to send delete request using python

问题

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

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

登录:

import requests,hmac,base64,struct,hashlib,time,mysql.connector
import win32com.client
import paramiko
import pyotp
import time

api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
response = requests.post(api_url, json=todo)
token = response.json().get('jwt_token').get('token')
time.sleep(45)

MFA代码:

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6)  # "6"是文件夹的索引 - 在这种情况下是收件箱。您可以更改该数字来引用任何其他文件夹
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print(body_content)
sub1="Console."
sub2="This"

idx1 = body_content.index(sub1)
idx2 = body_content.index(sub2)

res = ''
# 获取两个子字符串之间的内容
for idx in range(idx1 + len(sub1) + 1, idx2):
    res = res + body_content[idx]

# 打印结果
print("提取的字符串:" + res.strip())

mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"

todo = {"code": res.strip(), "email": "pchaubal+21022023SP@cytrio.com", "jwt_token": token}
print(todo)

headr = ""
print(headr)
response = requests.post(mfa_verify_url, json=todo, headers={'Authorization': 'Token ' + token})

删除请求:

for x in range(1):
    x = 2663
    session = requests.session()
    session.trust_env = True
    tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/" + str(x)
    headers = {'Authorization': f'Token {token}'}
    response = requests.delete(tenant_delete_url, headers=headers)

即使在删除请求中提供了标头我仍然收到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

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

MFA Code

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this     
case,
                                # the inbox. You can change that number to reference
                                # any other folder
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print (body_content)
sub1="Console."
sub2="This"

idx1 = body_content.index(sub1)
idx2 = body_content.index(sub2)

res = ''
# getting elements in between
for idx in range(idx1 + len(sub1) + 1, idx2):
    res = res + body_content[idx]

# printing result
print("The extracted string : " + res.strip())

mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"

todo = {"code":res.strip(), "email":"pchaubal+21022023SP@cytrio.com", "jwt_token":token}
print(todo)

headr = ""
print (headr)
response = requests.post(mfa_verify_url, json=todo, headers={'Authorization': 'Token     
'+token})

Delete request

for x in range (1):
    x=2663
    session=requests.session()
    session.trust_env = True
    tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/"+str(x)
    #headers = "{'Authorization': '" + token + "'}"
    headers = {'Authorization': f'Token {token}'}
    #print('Authorization '+headers)
    #response = requests.delete(tenant_delete_url, headers={'Authorization ': 
    f'{token}'})
    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 调用来解决了这个问题。以下是可运行的代码

导入请求、hmac、base64、struct、hashlib、time、mysql.connector
导入win32com.client
导入时间
api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
session = requests.session()
response = session.post(api_url, json=todo)
token = response.json().get('jwt_token').get('token')
time.sleep(45)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6) # "6" 指的是一个文件夹的索引 - 在这种情况下,
                                    # 收件箱。您可以更改该数字以引用
                                    # 任何其他文件夹
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print (body_content)
sub1="Console."
sub2="This"

idx1 = body_content.index(sub1)
idx2 = body_content.index(sub2)

res = ''
# 获取两者之间的元素
for idx in range(idx1 + len(sub1) + 1, idx2):
    res = res + body_content[idx]

# 打印结果
mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"
todo = {"code":res.strip(), "email":"pchaubal+21022023SP@cytrio.com", "jwt_token":token}
session.headers={'Authorization': 'Token '+token}
response = session.post(mfa_verify_url, json=todo)
print(response.json())
for x in range (1):
    x=13
    tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/"+str(x)
    headers = {'Authorization': f'Token {token}'}
    response = session.delete(tenant_delete_url, headers=headers)
    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

import requests,hmac,base64,struct,hashlib,time,mysql.connector
import win32com.client
import time
api_url="https://cpm-api-qa.aprivada.com/api/v1/users/login"
todo = {"email":"pchaubal+21022023SP@cytrio.com","password":"Cytrio@123"}
session = requests.session()
response = session.post(api_url, json=todo)
token = response.json().get('jwt_token').get('token')
time.sleep(45)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print (body_content)
sub1="Console."
sub2="This"

idx1 = body_content.index(sub1)
idx2 = body_content.index(sub2)

res = ''
# getting elements in between
for idx in range(idx1 + len(sub1) + 1, idx2):
    res = res + body_content[idx]

# printing result
mfa_verify_url = "https://cpm-api-qa.aprivada.com/api/v1/users/mfa/verify"
todo = {"code":res.strip(), "email":"pchaubal+21022023SP@cytrio.com", "jwt_token":token}
session.headers={'Authorization': 'Token '+token}
response = session.post(mfa_verify_url, json=todo)
print(response.json())
for x in range (1):
    x=13
    tenant_delete_url = "https://cpm-api-qa.aprivada.com/api/v1/tenants/"+str(x)
    headers = {'Authorization': f'Token {token}'}
    response = session.delete(tenant_delete_url, headers=headers)
    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:

确定