Skip to content

Commit

Permalink
Merge pull request #50 from alpacahq/feature/oauth-support
Browse files Browse the repository at this point in the history
Add alternative authentication via OAuth
  • Loading branch information
ttt733 authored Aug 14, 2019
2 parents 1d6480a + b306159 commit a53685a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions alpaca/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ var (
dataUrl = "https://data.alpaca.markets/"
apiVersion = "v2"
do = func(c *Client, req *http.Request) (*http.Response, error) {
req.Header.Set("APCA-API-KEY-ID", c.credentials.ID)
req.Header.Set("APCA-API-SECRET-KEY", c.credentials.Secret)
if c.credentials.OAuth != "" {
req.Header.Set("Authorization", "Bearer "+c.credentials.OAuth)
} else {
req.Header.Set("APCA-API-KEY-ID", c.credentials.ID)
req.Header.Set("APCA-API-SECRET-KEY", c.credentials.Secret)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions common/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ var (
const (
EnvApiKeyID = "APCA_API_KEY_ID"
EnvApiSecretKey = "APCA_API_SECRET_KEY"
EnvApiOAuth = "APCA_API_OAUTH"
)

type APIKey struct {
ID string
Secret string
OAuth string
}

// Credentials returns the user's Alpaca API key ID
Expand All @@ -26,5 +28,6 @@ func Credentials() *APIKey {
return &APIKey{
ID: os.Getenv(EnvApiKeyID),
Secret: os.Getenv(EnvApiSecretKey),
OAuth: os.Getenv(EnvApiOAuth),
}
}

0 comments on commit a53685a

Please sign in to comment.