/
google_search_console_site_url.go
47 lines (38 loc) · 1.05 KB
/
google_search_console_site_url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package validator
import (
"context"
"github.com/go-errors/errors"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/api/searchconsole/v1"
"github.com/th0th/poeticmetric/backend/pkg/depot"
"github.com/th0th/poeticmetric/backend/pkg/env"
"github.com/th0th/poeticmetric/backend/pkg/model"
)
func GoogleSearchConsoleSiteUrl(dp *depot.Depot, organizationId uint64, v string) bool {
ctx := context.Background()
modelOrganization := &model.Organization{}
err := dp.Postgres().
Model(&model.Organization{}).
Select("google_oauth_refresh_token").
Where("id = ?", organizationId).
First(modelOrganization).
Error
if err != nil {
panic(errors.Wrap(err, 0))
}
service, err := searchconsole.NewService(
ctx,
option.WithTokenSource(env.GetGoogleOauthConfig().TokenSource(ctx, &oauth2.Token{
RefreshToken: *modelOrganization.GoogleOauthRefreshToken,
})),
)
if err != nil {
return false
}
sitesGetCall, err := service.Sites.Get(v).Do()
if err != nil || sitesGetCall.SiteUrl != v {
return false
}
return true
}