Phase 3 part 1: Search Console and what already ranks
PLAN: Phase 3 rewritten around a weekly seven-job SEO loop (what ranks, next keywords, brief, page, approval, morning check, Monday report), with chatseo.app's ideas borrowed and rankings from Search Console only. Google OAuth (PKCE, offline, read-only webmasters scope) from Settings → Search engines, with each website's property. Search Console is synced into SQLite by day (site, page, and page+query; 16 months, then twice a day). SEO → What ranks shows the last 90 days: totals, the share of clicks from hidden queries, best pages, queries shown but not clicked, and the pages to protect and to push, each with its numbers. Schema 12. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+100
-2
@@ -1,5 +1,5 @@
|
||||
// Package oauth runs the OAuth 2.0 authorization-code flows for X and
|
||||
// LinkedIn. The web app sends the browser to AuthURL, and its callback
|
||||
// Package oauth runs the OAuth 2.0 authorization-code flows for X,
|
||||
// LinkedIn, Reddit and Google. The web app sends the browser to AuthURL, and its callback
|
||||
// handler calls Exchange with the code the provider sends back.
|
||||
package oauth
|
||||
|
||||
@@ -254,3 +254,101 @@ func randomString() (string, error) {
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
// --- Google (Search Console) ---
|
||||
|
||||
var (
|
||||
googleAuthURL = "https://accounts.google.com/o/oauth2/v2/auth"
|
||||
googleTokenURL = "https://oauth2.googleapis.com/token"
|
||||
)
|
||||
|
||||
// GoogleScopes: read-only Search Console, and the account's email to show
|
||||
// who is signed in.
|
||||
const GoogleScopes = "https://www.googleapis.com/auth/webmasters.readonly openid email"
|
||||
|
||||
// GoogleStart begins Google's authorization-code flow with PKCE, asking for
|
||||
// a refresh token (offline access) so the daily sync runs on its own.
|
||||
func GoogleStart(clientID, redirectURI string) (string, *Pending, error) {
|
||||
verifier, challenge, err := pkce()
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
state, err := randomString()
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
params := url.Values{
|
||||
"response_type": {"code"},
|
||||
"client_id": {clientID},
|
||||
"redirect_uri": {redirectURI},
|
||||
"scope": {GoogleScopes},
|
||||
"state": {state},
|
||||
"code_challenge": {challenge},
|
||||
"code_challenge_method": {"S256"},
|
||||
"access_type": {"offline"},
|
||||
"prompt": {"consent"}, // always return a refresh token
|
||||
}
|
||||
p := &Pending{Provider: "google", State: state, Verifier: verifier, RedirectURI: redirectURI, Started: time.Now()}
|
||||
return googleAuthURL + "?" + params.Encode(), p, nil
|
||||
}
|
||||
|
||||
// GoogleExchange swaps the callback's code for tokens.
|
||||
func GoogleExchange(ctx context.Context, clientID, clientSecret, code string, p *Pending) (*Result, error) {
|
||||
return googleToken(ctx, url.Values{
|
||||
"grant_type": {"authorization_code"},
|
||||
"code": {code},
|
||||
"redirect_uri": {p.RedirectURI},
|
||||
"code_verifier": {p.Verifier},
|
||||
"client_id": {clientID},
|
||||
"client_secret": {clientSecret},
|
||||
})
|
||||
}
|
||||
|
||||
// RefreshGoogle gets a new access token (good for an hour) from the refresh
|
||||
// token.
|
||||
func RefreshGoogle(ctx context.Context, clientID, clientSecret, refreshToken string) (*Result, error) {
|
||||
return googleToken(ctx, url.Values{
|
||||
"grant_type": {"refresh_token"},
|
||||
"refresh_token": {refreshToken},
|
||||
"client_id": {clientID},
|
||||
"client_secret": {clientSecret},
|
||||
})
|
||||
}
|
||||
|
||||
func googleToken(ctx context.Context, data url.Values) (*Result, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, googleTokenURL, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
return tokenResponse(req)
|
||||
}
|
||||
|
||||
var googleUserInfoURL = "https://openidconnect.googleapis.com/v1/userinfo"
|
||||
|
||||
// GoogleEmail is the signed-in account's email.
|
||||
func GoogleEmail(ctx context.Context, accessToken string) (string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, googleUserInfoURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
res, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("userinfo HTTP %d", res.StatusCode)
|
||||
}
|
||||
var out struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
err = json.NewDecoder(res.Body).Decode(&out)
|
||||
return out.Email, err
|
||||
}
|
||||
|
||||
// SetGoogleURLs points the Google flow at other servers (for tests).
|
||||
func SetGoogleURLs(auth, token, userinfo string) {
|
||||
googleAuthURL, googleTokenURL, googleUserInfoURL = auth, token, userinfo
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user