,"acmp3ts":False,"acwm":"maybe","acwmts":False,"ocpt":False,"vco":"","vcots":False,"vch":…
- User
,"acmp3ts":False,"acwm":"maybe","acwmts":False,"ocpt":False,"vco":"","vcots":False,"vch":"probably","vchts":True,"vcw":"probably","vcwts":True,"vc3":"maybe","vc3ts":False,"vcmp":"","vcmpts":False,"vcq":"maybe","vcqts":False,"vc1":"probably","vc1ts":True,"dvm":8,"sqt":False,"so":"landscape-primary","bda":False,"wdw":True,"prm":True,"tzp":True,"cvs":True,"usb":True,"cap":True,"tbf":False,"lgs":True,"tpd":True }), 'eventCounters': '[]', 'jsType': 'ch', 'cid': 'KOWn3t9QNk3dJJJEkpZJpspfb2HPZIVs0KSR7RYTscx5iO7o84cw95j40zFFG7mpfbKxmfhAOs~bM8Lr8cHia2JZ3Cq2LAn5k6XAKkONfSSad99Wu36EhKYyODGCZwae', 'ddk': 'AE3F04AD3F0D3A462481A337485081', 'Referer': 'https://account.garena.com/', 'request': '/', 'responsePage': 'origin', 'ddv': '4.35.4' } data = '&'.join(f'{k}={urllib.parse.quote(str(v))}' for k, v in payload.items()) try: response = session.post(url, headers=headers, data=data) response.raise_for_status() # Raise an exception for HTTP errors response_json = response.json() if response_json['status'] == 200 and 'cookie' in response_json: # Check the response status and if cookie is present cookie_string = response_json['cookie'] datadome = cookie_string.split(';')[0].split('=')[1] logging.info(f"Successfully retrieved datadome: {datadome}") # Log success return datadome else: logging.error(f"Error Status code: {response_json['status']}") # Log error if status is not 200 logging.error(f"Response: {response.text[:200]}...") # Log the first 200 characters of the response for debugging return None except requests.exceptions.RequestException as e: # Catch any request-related errors (e.g., network issues) logging.error(f"Error getting DataDome cookie: {e}") # Log request exceptions return None except Exception as e: # Catch any other exceptions logging.error(f"Unexpected error: {e}") # Log unexpected errors return None def prelogin(session, account): url = 'https://sso.garena.com/api/prelogin' params = { 'app_id': '10100', 'account': account, 'format': 'json', 'id': str(int(time.time() * 1000)) } headers = { 'accept': 'application/json, text/plain, /', 'accept-encoding': 'gzip, deflate, br, zstd', 'accept-language': 'en-US,en;q=0.9', 'cache-control': 'no-cache', 'connection': 'keep-alive', 'host': 'sso.garena.com', 'pragma': 'no-cache', 'referer': 'https://sso.garena.com/universal/login?app_id=10100&redirect_uri=https%3A%2F%2Faccount.garena.com%2F&locale=en-PH', 'sec-ch-ua': '"Chromium";v="130", "Microsoft Edge";v="130", "Not?A_Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0' } try: response = session.get(url, headers=headers, params=params) response.raise_for_status() # Raise an exception for HTTP errors data = response.json() new_datadome = response.cookies.get('datadome') # Retrieve the 'datadome' cookie if 'error' in data: # Check for any error in the response data logging.error(f"{Fore.RED}[ERROR]{Style.RESET_ALL} {account}: {data['error']}") return None, None, new_datadome # Return None for v1, v2 on error logging.info(f"{Fore.GREEN}[INFO]{Style.RESET_ALL} Prelogin successful for {account}") return data.get('v1'), data.get('v2'), new_datadome # Return v1, v2 and the new datadome cookie
- Assistant
It looks like you're working with Python code that interacts with the Garena SSO API and are dealing with the DataDome anti-bot protection mechanism. The code you've posted includes two functions:
- A function to retrieve the DataDome cookie (though the start of the function isn't shown in your snippet).
- The
preloginfunction, which performs a pre-login request to the Garena SSO API.
Based on your code snippet, here are some observations and suggestions:
1. DataDome Cookie Handling
DataDome is an anti-bot protection service. Websites using DataDome analyze requests and may block or challenge requests that appear automated or suspicious. The 'datadome' cookie is part of this process.
In your code, you're trying to retrieve the 'datadome' cookie after making a request:
new_datadome = response.cookies.get('datadome') # Retrieve the 'datadome' cookieSuggestions:
-
Check if the Cookie is Present: Ensure that the 'datadome' cookie is actually being set in the response. You can print out all cookies received to verify:
print(response.cookies) -
Handle Missing Cookie: If the 'datadome' cookie isn't present, the
getmethod will returnNone. You might want to add a check to handle this case. -
Session Persistence: Ensure that you're using the same session for subsequent requests so that cookies are retained.
2. Handling the Prelogin Response
In your
preloginfunction, you parse the JSON response and check for errors:data = response.json() # ... if 'error' in data: # Check for any error in the response data logging.error(f"{Fore.RED}[ERROR]{Style.RESET_ALL} {account}: {data['error']}") return None, None, new_datadome # Return None for v1, v2 on errorSuggestions:
-
Check the Response Structure: Make sure that the response JSON has the keys you're trying to access (
'v1','v2', etc.). You can print outdatato inspect its structure.print(data) -
Error Details: If there's an error, the
'error'key might contain more details. Ensure you're capturing and logging all the relevant information.
3. User-Agent and Headers
In your headers, you're setting a specific User-Agent and other headers:
headers = { # ... 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0' }Suggestions:
- Update User-Agent: The User-Agent string specifies a Chrome version (
130.0.0.0) which, as of my knowledge cutoff in September 2021, is a future version. Using a non-existent or incorrect User-Agent might raise suspicion. Consider using a current and commonly used User-Agent string.
4. Exception Handling
In your exception handling, you're catching
requests.exceptions.RequestExceptionand a generalException. Ensure that you're logging exceptions properly.Suggestions:
-
Log Exception Details: When logging exceptions, include the traceback for more detailed information.
logging.error(f"Error getting DataDome cookie: {e}", exc_info=True)
5. Full Code Context
Your code snippet seems to be part of a larger script. To provide more precise assistance, it would be helpful to see:
- The complete function where you're retrieving the DataDome cookie.
- How you're using the
preloginfunction in the rest of your code. - Any error messages or stack traces you're encountering.
6. Potential Challenges with DataDome
Bypassing or automating requests against a service protected by DataDome can be challenging, as it's designed to prevent automated scraping and bots.
Suggestions:
-
Review Terms of Service: Make sure that your activities comply with Garena's terms of service and legal policies.
-
Human Interaction Required: Some anti-bot mechanisms require solving CAPTCHAs or other human interactions. Automation might not be possible without proper handling.
-
Use Official APIs: If available, use official APIs provided by the service, which are intended for developer use and may not have the same restrictions.
7. Next Steps
To better assist you, please provide:
- Error Messages: Any specific errors or unexpected behaviors you're encountering.
- Detailed Logs: Outputs from your logging that indicate where the issue might be.
- Specific Questions: Clarify what you're trying to achieve and where you're getting stuck.
Example of Improved Code with Additional Debugging:
def prelogin(session, account): url = 'https://sso.garena.com/api/prelogin' params = { 'app_id': '10100', 'account': account, 'format': 'json', 'id': str(int(time.time() * 1000)) } headers = { 'accept': 'application/json, text/plain, */*', 'accept-encoding': 'gzip, deflate, br, zstd', 'accept-language': 'en-US,en;q=0.9', 'cache-control': 'no-cache', 'connection': 'keep-alive', 'host': 'sso.garena.com', 'pragma': 'no-cache', 'referer': 'https://sso.garena.com/universal/login?app_id=10100&redirect_uri=https%3A%2F%2Faccount.garena.com%2F&locale=en-PH', 'sec-ch-ua': '"Chromium";v="114", "Microsoft Edge";v="114", "Not?A_Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0' } try: response = session.get(url, headers=headers, params=params) response.raise_for_status() # Raise an exception for HTTP errors data = response.json() print(f"Response Data: {data}") # Debugging: Print response data new_datadome = response.cookies.get('datadome') # Retrieve the 'datadome' cookie print(f"DataDome Cookie: {new_datadome}") # Debugging: Print the DataDome cookie if 'error' in data: # Check for any error in the response data logging.error(f"[ERROR] {account}: {data['error']}") return None, None, new_datadome # Return None for v1, v2 on error logging.info(f"[INFO] Prelogin successful for {account}") return data.get('v1'), data.get('v2'), new_datadome # Return v1, v2 and the new datadome cookie except requests.exceptions.RequestException as e: logging.error(f"Request exception during prelogin: {e}", exc_info=True) return None, None, None except Exception as e: logging.error(f"Unexpected error during prelogin: {e}", exc_info=True) return None, None, NoneNote:
- Updated User-Agent: Changed to a more realistic version number.
- Additional Debugging Prints: Added prints to show response data and the DataDome cookie.
- Exception Logging with Traceback: Included
exc_info=Trueto log the traceback.
If you provide more details, I'll be happy to help you further troubleshoot the issue.