检查谷歌账号是否被风控
在日常运营中,批量管理或自动化使用谷歌账户时,经常需要确认账户是否可用(是否被封禁、密码是否正确、登录是否成功等)。本文将介绍几种主流的检测手段与工具,并通过示例讲解如何快速判断一个谷歌账户的可用性。
import imaplib
import socket
def check_imap(email, password, imap_server='imap.gmail.com', port=993, timeout=10):
socket.setdefaulttimeout(timeout)
try:
conn = imaplib.IMAP4_SSL(imap_server, port)
conn.login(email, password)
conn.logout()
return True
except imaplib.IMAP4.error as e:
print(f"登录失败:{e}")
return False
except socket.timeout:
print("连接超时")
return False
if __name__ == '__main__':
email = 'your_account@gmail.com'
password = 'your_password'
if check_imap(email, password):
print("IMAP 登录成功,账户可用")
else:
print("IMAP 登录失败,账户不可用或网络异常")
说明
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
import os, pickle
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
CRED_FILE = 'credentials.json'
TOKEN_FILE = 'token.pickle'
def get_service():
creds = None
if os.path.exists(TOKEN_FILE):
with open(TOKEN_FILE, 'rb') as f:
creds = pickle.load(f)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CRED_FILE, SCOPES)
creds = flow.run_console()
with open(TOKEN_FILE, 'wb') as f:
pickle.dump(creds, f)
return build('gmail', 'v1', credentials=creds)
def check_gmail_api():
service = get_service()
try:
# 简单调用:获取用户标签列表
labels = service.users().labels().list(userId='me').execute()
print("标签列表:", [lbl['name'] for lbl in labels.get('labels', [])])
return True
except Exception as e:
print("API 调用失败:", e)
return False
if __name__ == '__main__':
if check_gmail_api():
print("Gmail API 调用正常,账户可用")
else:
print("Gmail API 调用异常,可能凭据无效或账户异常")
说明
通过 IMAP/SMTP 登录检测、OAuth 2.0 Playground 以及 Gmail API 等多种工具和方法,可以高效、可靠地判断谷歌账户是否可用。针对不同场景,灵活组合多种手段,并结合限速与重试策略,既能保证检测准确性,也能避免因频繁请求导致的封锁风险。
最后推荐大家直接用三方网站直接检测