-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoauth.go
51 lines (45 loc) · 1.36 KB
/
oauth.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
48
49
50
51
package gopify
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
// AuthorizationUrl returns a URL to shopify's consent page that asks for permissions
// for the required scopes.
func (g *Gopify) AuthorizationUrl(shop string, state string) string {
query := url.Values{
"client_id": {g.ApiKey},
"redirect_uri": {g.RedirectUrl},
"scope": {strings.Join(g.Scopes, ",")},
"state": {state},
}
return fmt.Sprintf("https://%s/admin/oauth/authorize?%s", shop, query.Encode())
}
// AccessToken retrieves an access token from shopify authorization server
//
// code is The authorization code obtained by using an authorization server
func (g *Gopify) AccessToken(shop string, code string) (string, error) {
accessTokenPath := "admin/oauth/access_token"
accessTokenEndPoint := fmt.Sprintf("https://%s/%s", shop, accessTokenPath)
requestParams, err := json.Marshal(map[string]string{
"client_id": g.ApiKey,
"client_secret": g.ApiSecret,
"code": code,
})
if err != nil {
return "", nil
}
res, err := http.Post(accessTokenEndPoint, "application/json", bytes.NewBuffer(requestParams))
if err != nil {
return "", err
}
defer res.Body.Close()
resPayload := map[string]string{}
if err := json.NewDecoder(res.Body).Decode(&resPayload); err != nil {
return "", err
}
return resPayload["access_token"], nil
}