MCPcopy
hub / github.com/writefreely/writefreely / exchangeOauthCode

Method exchangeOauthCode

oauth_writeas.go:55–86  ·  view source on GitHub ↗
(ctx context.Context, code string)

Source from the content-addressed store, hash-verified

53}
54
55func (c writeAsOauthClient) exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error) {
56 form := url.Values{}
57 form.Add("grant_type", "authorization_code")
58 form.Add("redirect_uri", c.CallbackLocation)
59 form.Add("code", code)
60 req, err := http.NewRequest("POST", c.ExchangeLocation, strings.NewReader(form.Encode()))
61 if err != nil {
62 return nil, err
63 }
64 req.WithContext(ctx)
65 req.Header.Set("User-Agent", ServerUserAgent(""))
66 req.Header.Set("Accept", "application/json")
67 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
68 req.SetBasicAuth(c.ClientID, c.ClientSecret)
69
70 resp, err := c.HttpClient.Do(req)
71 if err != nil {
72 return nil, err
73 }
74 if resp.StatusCode != http.StatusOK {
75 return nil, errors.New("unable to exchange code for access token")
76 }
77
78 var tokenResponse TokenResponse
79 if err := limitedJsonUnmarshal(resp.Body, tokenRequestMaxLen, &tokenResponse); err != nil {
80 return nil, err
81 }
82 if tokenResponse.Error != "" {
83 return nil, errors.New(tokenResponse.Error)
84 }
85 return &tokenResponse, nil
86}
87
88func (c writeAsOauthClient) inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error) {
89 req, err := http.NewRequest("GET", c.InspectLocation, nil)

Callers

nothing calls this directly

Calls 3

ServerUserAgentFunction · 0.85
limitedJsonUnmarshalFunction · 0.85
DoMethod · 0.65

Tested by

no test coverage detected