Skip to content

Commit

Permalink
Merge pull request #205 from sbellity/use-authorization-header
Browse files Browse the repository at this point in the history
Add option to pass access_token in Authorization header instead of query parameter
  • Loading branch information
huandu authored Feb 27, 2023
2 parents 8516e6b + e5c0f29 commit 004c156
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type Session struct {
app *App
id string

enableAppsecretProof bool // add "appsecret_proof" parameter in every facebook API call.
appsecretProof string // pre-calculated "appsecret_proof" value.
enableAppsecretProof bool // add "appsecret_proof" parameter in every facebook API call.
appsecretProof string // pre-calculated "appsecret_proof" value.
useAuthorizationHeader bool // pass accessToken in headers instead of query params

debug DebugMode // using facebook debugging api in every request.

Expand Down Expand Up @@ -268,6 +269,11 @@ func (session *Session) SetAccessToken(token string) {
}
}

// UseAuthorizationHeader pass accessToken in authorization header instead of query params.
func (session *Session) UseAuthorizationHeader() {
session.useAuthorizationHeader = true
}

// AppsecretProof checks appsecret proof is enabled or not.
func (session *Session) AppsecretProof() string {
if !session.enableAppsecretProof {
Expand Down Expand Up @@ -429,8 +435,11 @@ func (session *Session) graphBatch(batchParams Params, params ...Params) ([]Resu
}

func (session *Session) prepareParams(params Params) {
if _, ok := params["access_token"]; !ok && session.accessToken != "" {
params["access_token"] = session.accessToken

if !session.useAuthorizationHeader {
if _, ok := params["access_token"]; !ok && session.accessToken != "" {
params["access_token"] = session.accessToken
}
}

if session.enableAppsecretProof && session.accessToken != "" && session.app != nil {
Expand Down Expand Up @@ -541,6 +550,10 @@ func (session *Session) sendRequest(request *http.Request) (response *http.Respo
request = request.WithContext(session.context)
}

if session.useAuthorizationHeader {
request.Header.Set("Authorization", "Bearer "+session.accessToken)
}

if session.HttpClient == nil {
response, err = http.DefaultClient.Do(request)
} else {
Expand Down

0 comments on commit 004c156

Please sign in to comment.