bukson / steampy

A Steam trading library for python 3
MIT License
559 stars 153 forks source link

is_session_alive (as well as other functions making requests to SteamUrl.COMMUNITY_URL) are not working properly. #374

Open ZRHann opened 6 months ago

ZRHann commented 6 months ago

When get_my_inventory correctly retrieved the inventory items, is_session_alive returned False. Below is my code:

import steampy
from steampy.client import SteamClient
from steampy.models import Asset, GameOptions

steam_client = SteamClient('MY_API_KEY')
steam_client.login('xxx', 'xxx', './xxx.maFile')
# steam_client.set_login_cookies(cookie)
print(steam_client.is_session_alive()) # False
print(steam_client.get_my_inventory(GameOptions.CS)) # the correct inventory
ZRHann commented 6 months ago

This is odd. Perhaps it has something to do with the headers?

ZRHann commented 6 months ago

And when I modified it to

    @login_required
    def is_session_alive(self) -> bool:
        steam_login = self.username
        headers = {
            'Cache-Control': 'max-age=0',
            'Sec-Ch-Ua': '"Not?A_Brand";v="8", "Chromium";v="108"',
            'Sec-Ch-Ua-Mobile': '?0',
            'Sec-Ch-Ua-Platform': "Windows",
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Sec-Fetch-Site': 'none',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-User': '?1',
            'Sec-Fetch-Dest': 'document',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
        }
        main_page_response = self._session.get(SteamUrl.COMMUNITY_URL, headers=headers)
        return steam_login.lower() in main_page_response.text.lower()

it was able to return True correctly.

ZRHann commented 6 months ago

Through the process of elimination, I found that adding Accept to the headers can fix this issue. Namely:

headers = {
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
}