feat: Implement S2A OAuth authorization using chromedp and rod browser automation, and integrate it into team processing.
This commit is contained in:
@@ -164,6 +164,7 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
|
||||
var currentURL string
|
||||
_ = chromedp.Run(ctx, chromedp.Location(¤tURL))
|
||||
logStep(StepNavigate, "页面加载完成 | URL: %s", currentURL)
|
||||
|
||||
if strings.Contains(currentURL, "code=") {
|
||||
logStep(StepComplete, "授权成功(快速通道)")
|
||||
@@ -194,6 +195,7 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
)
|
||||
if err == nil {
|
||||
emailFilled = true
|
||||
logStep(StepInputEmail, "邮箱已填写 | 选择器: %s", sel)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -201,7 +203,8 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
findCancel()
|
||||
|
||||
if !emailFilled {
|
||||
logError(StepInputEmail, "未找到邮箱输入框")
|
||||
_ = chromedp.Run(ctx, chromedp.Location(¤tURL))
|
||||
logError(StepInputEmail, "未找到邮箱输入框 | URL: %s", currentURL)
|
||||
return "", fmt.Errorf("未找到邮箱输入框")
|
||||
}
|
||||
|
||||
@@ -209,6 +212,7 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
|
||||
buttonSelectors := []string{
|
||||
`button[type="submit"]`,
|
||||
`div._ctas_1alro_13 button`,
|
||||
`button[data-testid="login-button"]`,
|
||||
`button.continue-btn`,
|
||||
`input[type="submit"]`,
|
||||
@@ -235,13 +239,15 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
return ExtractCodeFromCallbackURL(currentURL), nil
|
||||
}
|
||||
|
||||
logStep(StepInputPassword, "查找密码框 | URL: %s", currentURL)
|
||||
|
||||
// 密码输入框选择器
|
||||
passwordSelectors := []string{
|
||||
`input[name="current-password"]`,
|
||||
`input[autocomplete="current-password"]`,
|
||||
`input[type="password"]`,
|
||||
`input[name="password"]`,
|
||||
`input[name="current-password"]`,
|
||||
`input[id="password"]`,
|
||||
`input[autocomplete="current-password"]`,
|
||||
}
|
||||
|
||||
// 使用短超时查找密码框(10秒)
|
||||
@@ -257,6 +263,7 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
)
|
||||
if err == nil {
|
||||
passwordFilled = true
|
||||
logStep(StepInputPassword, "密码已填写 | 选择器: %s", sel)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -264,7 +271,8 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
findCancel2()
|
||||
|
||||
if !passwordFilled {
|
||||
logError(StepInputPassword, "未找到密码输入框")
|
||||
_ = chromedp.Run(ctx, chromedp.Location(¤tURL))
|
||||
logError(StepInputPassword, "未找到密码输入框 | URL: %s", currentURL)
|
||||
return "", fmt.Errorf("未找到密码输入框")
|
||||
}
|
||||
|
||||
@@ -295,8 +303,14 @@ func CompleteWithChromedpLogged(authURL, email, password, teamID string, headles
|
||||
}
|
||||
|
||||
if strings.Contains(url, "consent") {
|
||||
logStep(StepConsent, "处理授权同意...")
|
||||
for _, sel := range buttonSelectors {
|
||||
logStep(StepConsent, "处理授权同意... | URL: %s", url)
|
||||
// 同意页面的确认按钮(第二个按钮)
|
||||
consentSelectors := []string{
|
||||
`div._ctas_1alro_13 div:nth-child(2) button`,
|
||||
`button[type="submit"]`,
|
||||
`div._ctas_1alro_13 button`,
|
||||
}
|
||||
for _, sel := range consentSelectors {
|
||||
err = chromedp.Run(ctx, chromedp.Click(sel, chromedp.ByQuery))
|
||||
if err == nil {
|
||||
break
|
||||
|
||||
@@ -218,6 +218,11 @@ func (r *RodAuth) CompleteOAuthLogged(authURL, email, password, teamID string, l
|
||||
|
||||
page.MustWaitDOMStable()
|
||||
|
||||
// 获取当前URL
|
||||
info, _ := page.Info()
|
||||
currentURL := info.URL
|
||||
logStep(StepNavigate, "页面加载完成 | URL: %s", currentURL)
|
||||
|
||||
if code := r.checkForCode(page); code != "" {
|
||||
logStep(StepComplete, "授权成功(快速通道)")
|
||||
return code, nil
|
||||
@@ -226,14 +231,17 @@ func (r *RodAuth) CompleteOAuthLogged(authURL, email, password, teamID string, l
|
||||
// 使用10秒超时查找邮箱输入框
|
||||
emailInput, err := page.Timeout(10 * time.Second).Element("input[name='email'], input[type='email'], input[name='username'], input[id='email'], input[autocomplete='email']")
|
||||
if err != nil {
|
||||
logError(StepInputEmail, "未找到邮箱输入框")
|
||||
info, _ := page.Info()
|
||||
logError(StepInputEmail, "未找到邮箱输入框 | URL: %s", info.URL)
|
||||
return "", fmt.Errorf("未找到邮箱输入框")
|
||||
}
|
||||
|
||||
logStep(StepInputEmail, "邮箱已填写")
|
||||
emailInput.MustSelectAllText().MustInput(email)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit'], button[name='action']"); btn != nil {
|
||||
// 点击提交按钮
|
||||
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit'], div._ctas_1alro_13 button, button[name='action']"); btn != nil {
|
||||
btn.MustClick()
|
||||
}
|
||||
|
||||
@@ -244,18 +252,24 @@ func (r *RodAuth) CompleteOAuthLogged(authURL, email, password, teamID string, l
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// 使用10秒超时查找密码输入框
|
||||
passwordInput, err := page.Timeout(10 * time.Second).Element("input[type='password'], input[name='password'], input[id='password']")
|
||||
// 获取当前URL用于调试
|
||||
info, _ = page.Info()
|
||||
logStep(StepInputPassword, "查找密码框 | URL: %s", info.URL)
|
||||
|
||||
// 使用10秒超时查找密码输入框(优先使用 current-password)
|
||||
passwordInput, err := page.Timeout(10 * time.Second).Element("input[name='current-password'], input[autocomplete='current-password'], input[type='password'], input[name='password'], input[id='password']")
|
||||
if err != nil {
|
||||
logError(StepInputPassword, "未找到密码输入框")
|
||||
info, _ := page.Info()
|
||||
logError(StepInputPassword, "未找到密码输入框 | URL: %s", info.URL)
|
||||
return "", fmt.Errorf("未找到密码输入框")
|
||||
}
|
||||
|
||||
logStep(StepInputPassword, "密码已填写")
|
||||
passwordInput.MustSelectAllText().MustInput(password)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
logStep(StepSubmitPassword, "正在登录...")
|
||||
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit'], button[name='action']"); btn != nil {
|
||||
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit'], div._ctas_1alro_13 button, button[name='action']"); btn != nil {
|
||||
btn.MustClick()
|
||||
}
|
||||
|
||||
@@ -272,8 +286,9 @@ func (r *RodAuth) CompleteOAuthLogged(authURL, email, password, teamID string, l
|
||||
currentURL := info.URL
|
||||
|
||||
if strings.Contains(currentURL, "consent") {
|
||||
logStep(StepConsent, "处理授权同意...")
|
||||
if btn, _ := page.Timeout(500 * time.Millisecond).Element("button[type='submit']"); btn != nil {
|
||||
logStep(StepConsent, "处理授权同意... | URL: %s", currentURL)
|
||||
// 同意页面的确认按钮(第二个按钮)
|
||||
if btn, _ := page.Timeout(500 * time.Millisecond).Element("div._ctas_1alro_13 div:nth-child(2) button, button[type='submit'], div._ctas_1alro_13 button"); btn != nil {
|
||||
btn.Click(proto.InputMouseButtonLeft, 1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user