Skip to content

Commit 7de5313

Browse files
authored
Merge pull request #5569 from segmentio/MichaelGHSeg/oauth
OAuth 2.0 new SDK feature documentation
2 parents 7be8c8b + e65b49b commit 7de5313

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/connections/sources/catalog/libraries/server/node/index.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,46 @@ analytics.track({
653653
```
654654
655655
656+
## OAuth 2.0
657+
658+
> info ""
659+
> OAuth 2.0 is currently in private beta and is governed by Segment’s [First Access and Beta Preview Terms](https://www.twilio.com/en-us/legal/tos){:target="_blank"}.
660+
661+
Enable [OAuth 2.0](/docs/connections/oauth/) in your Segment workspace to guarantee authorized communication between your server environment and Segment's Tracking API. To support the non-interactive server environment, the OAuth workflow used is a signed client assertion JWT.
662+
663+
You will need a public and private key pair where:
664+
- The public key is uploaded to the Segment dashboard.
665+
- The private key is kept in your server environment to be used by this SDK.
666+
667+
Your server will verify its identity by signing a token request and will receive a token that is used to to authorize all communication with the Segment Tracking API.
668+
669+
You'll need to provide the OAuth Application ID and the public key's ID, both of which are provided in the Segment dashboard. There are also options available to specify the authorization server, custom scope, maximum number of retries, or a custom HTTP client if your environment has special rules for separate Segment endpoints.
670+
671+
Be sure to implement handling for Analytics SDK errors. Good logging helps distinguish any configuration issues.
672+
673+
For more information, see the [Segment OAuth 2.0 documentation](/docs/connections/oauth/).
674+
675+
```ts
676+
import { Analytics, OAuthSettings } from '@segment/analytics-node';
677+
import { readFileSync } from 'fs'
678+
679+
const privateKey = readFileSync('private.pem', 'utf8')
680+
681+
const settings: OAuthSettings = {
682+
clientId: '<CLIENT_ID_FROM_DASHBOARD>',
683+
clientKey: privateKey,
684+
keyId: '<PUB_KEY_ID_FROM_DASHBOARD>',
685+
}
686+
687+
const analytics = new Analytics({
688+
writeKey: '<MY_WRITE_KEY>',
689+
oauthSettings: settings,
690+
})
691+
692+
analytics.on('error', (err) => { console.error(err) })
693+
694+
analytics.track({ userId: 'foo', event: 'bar' })
695+
```
656696
## Troubleshooting
657697
658698
{% include content/troubleshooting-intro.md %}

src/connections/sources/catalog/libraries/server/python/index.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,49 @@ analytics.write_key = 'YOUR_WRITE_KEY'
530530

531531
## Google App Engine
532532

533-
Google App Engine my not resolve project dependencies. If this is the case add the following to your project alongside analytics-python:
533+
Google App Engine may not resolve project dependencies. If this is the case add the following to your project alongside analytics-python:
534534
- [requests](https://github.com/kennethreitz/requests){:target="_blank"}
535535
- python-dateutil](https://github.com/paxan/python-dateutil){:target="_blank"}
536536

537537
If you're having issues with threads outliving your request, check [Background threads and synchronous mode](#background-threads-and-synchronous-mode)
538538

539+
## OAuth 2.0
540+
541+
> info ""
542+
> OAuth 2.0 is currently in private beta and is governed by Segment’s [First Access and Beta Preview Terms](https://www.twilio.com/en-us/legal/tos){:target="_blank"}.
543+
544+
Enable [OAuth 2.0](/docs/connections/oauth/) in your Segment workspace to guarantee authorized communication between your server environment and Segment's Tracking API. To support the non-interactive server environment, the OAuth workflow used is a signed client assertion JWT.
545+
546+
You will need a public and private key pair where:
547+
- The public key is uploaded to the Segment dashboard.
548+
- The private key is kept in your server environment to be used by this SDK.
549+
Your server will verify its identity by signing a token request and will receive a token that is used to to authorize all communication with the Segment Tracking API.
550+
551+
You'll need to provide the OAuth Application ID and the public key's ID, both of which are provided in the Segment dashboard. There are also options available to specify the authorization server, custom scope, maximum number of retries, or a custom HTTP client if your environment has special rules for separate segment endpoints.
552+
553+
Be sure to implement handling for Analytics SDK errors. Good logging will help distinguish any configuration issues.
554+
555+
For more information, see the [Segment OAuth 2.0 documentation](/docs/connections/oauth/).
556+
557+
```python
558+
import segment.analytics as analytics
559+
with open("private_key.pem") as f:
560+
privatekey = f.read()
561+
562+
analytics.write_key = '<YOUR WRITE KEY HERE>'
563+
564+
analytics.oauth_client_id = 'CLIENT_ID' # OAuth application ID from segment dashboard
565+
analytics.oauth_client_key = privatekey # generated as a public/private key pair in PEM format from OpenSSL
566+
analytics.oauth_key_id = 'KEY_ID' # From segment dashboard after uploading public key
567+
568+
def on_error(error, items):
569+
print("An error occurred: ", error)
570+
analytics.on_error = on_error
571+
572+
analytics.track('AUser', 'track')
573+
analytics.flush()
574+
```
575+
539576
## Troubleshooting
540577

541578
### Request size limits

0 commit comments

Comments
 (0)