Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to pass access_token in Authorization header instead of query parameter #205

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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