Chào mừng bạn đến với website phapsummo.net

Trang chủ Đơn hàng Thông tin

Chia sẻ cách lấy RefreshToken từ tài khoản Hotmail/Outlook

Hiện nay, khi mua tài khoản Hotmail/Outlook, bạn thường nhận được các thông tin như Email, Password, và cả những thứ có vẻ lạ lẫm như Refresh Token, Client ID. Vậy, Refresh Token và Client ID là gì?

Refresh Token và Client ID là gì?

  • Refresh Token: Đây là một "chìa khóa" đặc biệt, cho phép ứng dụng của bạn truy cập tài khoản mà không cần phải nhập lại mật khẩu mỗi lần. Thay vì nhập mật khẩu, bạn có thể sử dụng Refresh Token để lấy một Access Token (chìa khóa ngắn hạn) để thực hiện các thao tác như đọc, gửi email.
  • Client ID: Tưởng tượng Client ID như một "giấy phép" để ứng dụng hoặc phần mềm của bạn được phép giao tiếp với dịch vụ của Microsoft (trong trường hợp này là Hotmail/Outlook). Nó cho phép Microsoft xác định được ứng dụng nào đang yêu cầu truy cập.

Tại sao Refresh Token và Client ID lại quan trọng?

  • Hiệu suất cao: Bạn có thể tự động hóa các tác vụ như đọc, gửi email mà không cần đăng nhập thủ công trên trình duyệt. Điều này tiết kiệm rất nhiều thời gian, đặc biệt khi bạn quản lý nhiều tài khoản.
  • Bảo mật: Refresh Token an toàn hơn so với việc lưu trữ mật khẩu trực tiếp trong ứng dụng.
  • Tự động hóa: Dễ dàng tích hợp vào các ứng dụng, script để tự động quản lý tài khoản.

Tuy nhiên, có một điều cần lưu ý: Refresh Token có thời hạn. Khi hết hạn, bạn sẽ cần một Refresh Token mới. Để giúp bạn dễ dàng có được Refresh Token, mình sẽ chia sẻ cách lấy Refresh Token bằng Python chỉ với vài request đơn giản.

Code:

---

from urllib.parse import urlencode
import re
import requests

class GetOAuth2Token:
   def __init__(self):
       self.client_id = "9e5f94bc-e8a4-4e73-b8be-63364c29d753"
       self.redirect_uri = "https://localhost"
       self.base_url = "https://login.live.com"
       self.token_url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
       
   def _get_headers(self, additional_headers: dict = None):
       headers = {
           'accept': '*/*',
           'accept-encoding': 'gzip, deflate, br',
           'accept-language': 'en-US,en;q=0.9',
           'sec-ch-ua': '"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
           '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; rv:128.0) Gecko/20100101 Thunderbird/128.2.3'
       }
       if additional_headers:
           headers.update(additional_headers)
       return headers

   def _handle_consent_page(self, post_url: str, resp_content: str, cookies: dict):
       post_headers = self._get_headers({'content-type': "application/x-www-form-urlencoded"})
       
       matches = re.finditer("        form_data = {match.group(1): match.group(3) for match in matches}
       
       encoded_data = urlencode(form_data)
       requests.post(post_url, data=encoded_data, headers=post_headers, cookies=cookies)
       
       form_data["ucaction"] = "Yes"
       encoded_data = urlencode(form_data)
       consent_resp = requests.post(post_url, data=encoded_data, headers=post_headers, 
                                  cookies=cookies, allow_redirects=False)
       
       redirect_url = consent_resp.headers.get('Location')
       final_resp = requests.post(redirect_url, data=encoded_data, headers=post_headers, 
                                cookies=cookies, allow_redirects=False)
       return final_resp.headers.get('Location')

   def run(self, email: str, password: str):
       auth_url = f"{self.base_url}/oauth20_authorize.srf"
       params = {
           'response_type': 'code',
           'client_id': self.client_id,
           'redirect_uri': self.redirect_uri,
           'scope': 'offline_access Mail.ReadWrite',
           'login_hint': email
       }
       auth_url = f"{auth_url}?{urlencode(params)}"
       
       headers = self._get_headers()
       post_headers = self._get_headers({'content-type': "application/x-www-form-urlencoded"})
       
       resp = requests.get(auth_url, headers=headers)
       
       post_url = f"{self.base_url}/ppsecure/post.srf" + re.search("https://login.live.com/ppsecure/post.srf?(.*?)',", resp.text).group(1)
       ppft = re.search("        
       login_data = {
           'ps': '2', 'PPFT': ppft, 'PPSX': 'Passp', 'NewUser': '1',
           'login': email, 'loginfmt': email, 'passwd': password,
           'type': '11', 'LoginOptions': '1', 'i13': '1',
           'CookieDisclosure': '0', 'IsFidoSupported': '1'
       }
       
       login_resp = requests.post(post_url, data=login_data, headers=post_headers, 
                                cookies=resp.cookies.get_dict(), allow_redirects=False)
       redirect_url = login_resp.headers.get('Location')
       
       # Handle consent if needed
       if not redirect_url:
           match = re.search("id=\"fmHF\" action=\"(.*?)\"", login_resp.text)
           if not match:
               return None
               
           post_url = match.group(1)
           if "Update?mkt=" in post_url:
               redirect_url = self._handle_consent_page(post_url, login_resp.text, login_resp.cookies.get_dict())
           elif "confirm?mkt=" in post_url:
               print("TODO: Xử lý confirm?mkt= - Nhập mã bảo mật")
               return None
           elif "Add?mkt=" in post_url:
               print("TODO: Xử lý Add?mkt= - Nhập mail khôi phục")
               return None
       
       # Get access token
       if redirect_url:
           code = redirect_url.split('=')[1]
           token_data = {
               'code': code,
               'client_id': self.client_id,
               'redirect_uri': self.redirect_uri,
               'grant_type': 'authorization_code'
           }
           token_resp = requests.post(self.token_url, data=token_data, headers=post_headers)
           return token_resp.json()
       return None


if __name__ == "__main__":
   auth = GetOAuth2Token()
   data = "abcxyz@outlook.com|123456" # email|password
   email, password = data.split('|')
   result = auth.run(email, password)
   print(result)

---

Đó là toàn bộ hướng dẫn về Refresh Token và Client ID trong Hotmail/Outlook và cách lấy Refresh Token bằng Python. Hy vọng bài viết này hữu ích cho bạn. Cảm ơn bạn đã theo dõi và chúc bạn một ngày làm việc hiệu quả!

Nhân tiện, bên mình cũng là 1 trong các shop cung cấp tài khoản Hotmail/Outlook chất lượng nhất Taphoammo, hãy qua ủng hộ mình nhé!!!