Python – POST request returning 401 response

I am trying to send a POST request to api url 'https://mpapi.tcgplayer.com/v3/login/signin' to login but each time I send the request I am getting 401 (Unauthorized) response although I am sending the request with proper credentials but stuck at 401. How can I solve this 401 error an get 200 response code from the server? Here is my minimal code:

import requests

cookies = {
    'valid': 'set=true',
    'optimizelyEndUserId': 'oeu1693344615632r0.665712861305519',
    'tracking-preferences': '{%22version%22:1%2C%22destinations%22:{%22Actions%20Amplitude%22:true%2C%22AdWords%22:true%2C%22Drip%22:true%2C%22Facebook%20Pixel%22:true%2C%22Google%20AdWords%20New%22:true%2C%22Google%20Enhanced%20Conversions%22:true%2C%22Google%20Tag%20Manager%22:true%2C%22Hotjar%22:true%2C%22Optimizely%22:true}%2C%22custom%22:{%22advertising%22:true%2C%22functional%22:true%2C%22marketingAndAnalytics%22:true}}',
    '_fbp': 'fb.1.1693344618532.604847361',
    'TCG_Data': 'M=1&SearchGameNameID=magic',
    '_hjSessionUser_1176217': 'eyJpZCI6ImU5MGY1NjBhLWIwMGItNTE0Yy05YTNiLTMyMWUxOGViN2FkMyIsImNyZWF0ZWQiOjE2OTMzNDQ2MTgxNTMsImV4aXN0aW5nIjp0cnVlfQ==',
    '__ssid': 'bf32fabbfc7c2ba1d8737b5a6235ea0',
    '_gid': 'GA1.2.1195753555.1693532743',
    'setting': 'CD=US&M=1',
    'ASP.NET_SessionId': 'bugrple5u0n5feotco4hgcy5',
    '_hjIncludedInSessionSample_1176217': '0',
    '_hjSession_1176217': 'eyJpZCI6ImI3MWQzMTgzLTNlYTMtNGIzMi04OTg4LWVmMzVmMjQwOTk5ZSIsImNyZWF0ZWQiOjE2OTM1NDQ5MDU3NjAsImluU2FtcGxlIjpmYWxzZX0=',
    '_hjAbsoluteSessionInProgress': '0',
    'BuylistCart_PRODUCTION': 'Ignore=true',
    'TCG_VisitorKey': '89d8d91f-12cf-4de2-8b0d-a12f6875677c',
    '_ga': 'GA1.1.1619037424.1693344619',
    'tcg_analytics_previousPageData': '%7B%22title%22%3A%22Order%20History%20-%20Online%20Gaming%20Store%20for%20Cards%2C%20Miniatures%2C%20Singles%2C%20Packs%20%26%20Booster%20Boxes%22%2C%22href%22%3A%22https%3A%2F%2Fstore.tcgplayer.com%2Fmyaccount%2Forderhistory%3FPageNumber%3D4%22%7D',
    'tcg-segment-session': '1693544965122%257C1693544965132',
    'ajs_user_id': '3a9bf8fc-1902-4ee1-9b4f-ed563d20cdfb',
    'ajs_anonymous_id': 'c815bf47-b9f5-454b-85b0-eac06b858db2',
    'analytics_session_id': '1693544965597',
    'analytics_session_id.last_access': '1693544965669',
    '_drip_client_4160913': 'vid%253D7c357fbb65b64b929bfb4c2b59cd284f%2526pageViews%253D28%2526sessionPageCount%253D4%2526lastVisitedAt%253D1693544965993%2526weeklySessionCount%253D5%2526lastSessionAt%253D1693544906361',
    '_ga_VS9BE2Z3GY': 'GS1.1.1693544906.9.1.1693544966.60.0.0',
    '_gcl_au': '1.1.2054494333.1693344618.406965533.1693544906.1693544975',
}

headers = {
    'authority': 'mpapi.tcgplayer.com',
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'en,ru;q=0.9',
    'content-type': 'application/json',
    # 'cookie': 'valid=set=true; optimizelyEndUserId=oeu1693344615632r0.665712861305519; tracking-preferences={%22version%22:1%2C%22destinations%22:{%22Actions%20Amplitude%22:true%2C%22AdWords%22:true%2C%22Drip%22:true%2C%22Facebook%20Pixel%22:true%2C%22Google%20AdWords%20New%22:true%2C%22Google%20Enhanced%20Conversions%22:true%2C%22Google%20Tag%20Manager%22:true%2C%22Hotjar%22:true%2C%22Optimizely%22:true}%2C%22custom%22:{%22advertising%22:true%2C%22functional%22:true%2C%22marketingAndAnalytics%22:true}}; _fbp=fb.1.1693344618532.604847361; TCG_Data=M=1&SearchGameNameID=magic; _hjSessionUser_1176217=eyJpZCI6ImU5MGY1NjBhLWIwMGItNTE0Yy05YTNiLTMyMWUxOGViN2FkMyIsImNyZWF0ZWQiOjE2OTMzNDQ2MTgxNTMsImV4aXN0aW5nIjp0cnVlfQ==; __ssid=bf32fabbfc7c2ba1d8737b5a6235ea0; _gid=GA1.2.1195753555.1693532743; setting=CD=US&M=1; ASP.NET_SessionId=bugrple5u0n5feotco4hgcy5; _hjIncludedInSessionSample_1176217=0; _hjSession_1176217=eyJpZCI6ImI3MWQzMTgzLTNlYTMtNGIzMi04OTg4LWVmMzVmMjQwOTk5ZSIsImNyZWF0ZWQiOjE2OTM1NDQ5MDU3NjAsImluU2FtcGxlIjpmYWxzZX0=; _hjAbsoluteSessionInProgress=0; BuylistCart_PRODUCTION=Ignore=true; TCG_VisitorKey=89d8d91f-12cf-4de2-8b0d-a12f6875677c; _ga=GA1.1.1619037424.1693344619; tcg_analytics_previousPageData=%7B%22title%22%3A%22Order%20History%20-%20Online%20Gaming%20Store%20for%20Cards%2C%20Miniatures%2C%20Singles%2C%20Packs%20%26%20Booster%20Boxes%22%2C%22href%22%3A%22https%3A%2F%2Fstore.tcgplayer.com%2Fmyaccount%2Forderhistory%3FPageNumber%3D4%22%7D; tcg-segment-session=1693544965122%257C1693544965132; ajs_user_id=3a9bf8fc-1902-4ee1-9b4f-ed563d20cdfb; ajs_anonymous_id=c815bf47-b9f5-454b-85b0-eac06b858db2; analytics_session_id=1693544965597; analytics_session_id.last_access=1693544965669; _drip_client_4160913=vid%253D7c357fbb65b64b929bfb4c2b59cd284f%2526pageViews%253D28%2526sessionPageCount%253D4%2526lastVisitedAt%253D1693544965993%2526weeklySessionCount%253D5%2526lastSessionAt%253D1693544906361; _ga_VS9BE2Z3GY=GS1.1.1693544906.9.1.1693544966.60.0.0; _gcl_au=1.1.2054494333.1693344618.406965533.1693544906.1693544975',
    'origin': 'https://www.tcgplayer.com',
    'referer': 'https://www.tcgplayer.com/',
    'sec-ch-ua': '"Chromium";v="112", "YaBrowser";v="23", "Not:A-Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Linux"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 YaBrowser/23.5.1.575 (beta) Yowser/2.5 Safari/537.36',
}

params = {
    'mpfev': '1773',
}

json_data = {
    'username': 'username',
    'password': 'password',
    'captchaToken': 'P1_eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.hKdwYXNza2V5xQQQlEAh4pjgO6V9dtxoFcYH8olq-Sy6nRL744Q3YrqsB0AzgFNNzY4l2wBGpZV23crZf0j-UFSyup2-Mk_TIEsyouvhi-8NRQ9YlCc6TnSE8QcXzdguyxNd6VJ9RWC9fQVOqVMdIOyAzAfxtKFJQOpv1WLxEipf0eD8ZCbDJX2tCDRkRbruSV07gOwo2leNuXsw6RwF9AOeh-9uM0qkZbrN86XwfhjEdTVEVKJwiI2wdWhTbdwOxeD2pA02Qy5xNzsuT9_N-duO8Omoty2-zYUmT6q8BMExYb-2ZNekSDDFff9DFvDyJO9L_juUo0Re8bULouoB9NnTV6gLOakdO-rufNDMsCAh-EqPkhP92XYuS_cBTcKV6RCWERfTfEGyVZcJphcZuP83YyPKX9ZtPmpT_yAqqcIX3n1MmbhPx59S49FsXNeItQPQlmoNJ8KRizcxI3dYoTEFSOCqpm5FUV2zvfKMJcLUUDeP2U2Kbi4csTZDD7BgUw8Pd94cd-s-dfs23e2hmRGucmvJUOMTS2ZGdFmxEgCirnyEc69bd38u-mkf9JpeahJaxD35IpJPnftZoFl18oezWUK_xYdiGPKUcR6avndPcDoWTKTfweqkR5dyRZoWoo63OjgLaxp6eVU0ZfSSMXHpYavFRiNjCZ_ITXg86Vzy0VusTpeI84CYb8Cku1u-sQGhDuJOyAAPMXMqFKR6MlDQzQanUJ62i8xbuEw-9XDGpffq3O8AbDrSD8hPp7Oth9bZrMyAqMOsdPERardKO6HTrvKFhv2MwuyxEfCRjuUovi7wps2tqVVXBYChoK0lGhit1rgAC2vaFTmgA23nNW7KsjSumjKqGjRmMjazgjGXah4eoYzTyEi6RIyq_JOQtf2Jg7IZ7ZTooGk51YYFGsS7N1SR3n0b-ajuPVp82MkVSqZpDOFUSMzVdqXrmKT_3n4UfdHwP4s87mqCw435ijEih5QJPdG9Ik-kbz6wveG0IY_LhfDVZEdhuZCbB1X2I1MbnL2y9uGrxf4t_sedo3t8zeHRSqfBGUEoJ4Hlv8FUpdBTZ9wwzOXRQ12mo4825Qp81pc_WTvR66xY3RhdslJ-NwbsAI2gngz9gajSNe5FI61VKaFhZzfISN-raeHBuc4Y2FPa2uNHDJfXhMM68akEXuFJViIKGOss09lOe2lcdMQoeR1aY504BSxOSoduoLjIy8ARGvZ6zq4MbxFK4iWKB1oSfayN9lL3G2qS1qsQZe1ni8X8D0hB687OdOpxWyfYrukftaQh9dmF4LwHwsgQLVY_LTyX1K3II8JXm13RDOifdPO1yH8qzr8cxupDZ6nLQ5HA06gl0Mlmv7Jif_l7JFp8OOCcwhtGhQTNOPYVIoWkNn0IftF41lujZXhwzmTxcoioc2hhcmRfaWTOFZnkVKJwZAA.PHlRGGsgsdv7gp4QWtD-6dax-gh9fENDWsSn--cGr6A',
    'validation': False,
    'key': None,
    'isRevalidation': False,
    'isLongTermRevalidation': False,
    'isMobileAppLogin': False,
}

response = requests.post(
    'https://mpapi.tcgplayer.com/v3/login/signin',
    params=params,
    cookies=cookies,
    headers=headers,
    json=json_data,
)

Is there anything in the request setup that might be causing the error? Thanks!

Leave a Comment