Custom Credentials
You can use a custom OAuth app to authenticate with a service account or a user account. See Using OAuth Authentication for more information.
User Accounts
Desktop Authentication with a Custom OAuth App
Follow the steps below to authenticate with the credentials for a custom OAuth app. See Creating a Custom OAuth App.Get and Refresh the OAuth Access Token
After setting the following, you are ready to connect:
- OAuthClientId: Set this to the client Id assigned when you registered your app.
- OAuthClientSecret: Set this to the client secret assigned when you registered your app.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- ChannelId: Set this to the Id of a YouTube channel. If not specified, data is returned for the authenticated user's channel.
- ContentOwnerId: Set this if you want to generate content owner reports.
- Extracts the access token from the callback URL and authenticates requests.
- Refreshes the access token when it expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Web Authentication with a Custom OAuth App
When connecting via a Web application, you need to register a custom OAuth app with YouTube Analytics. You can then use the driver to get and manage the OAuth token values.
Get an OAuth Access Token
Set the following connection properties to obtain the OAuthAccessToken:
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
You can then call stored procedures to complete the OAuth exchange:
- Call the GetOAuthAuthorizationURL stored procedure. Set the AuthMode input to WEB and set the CallbackURL input to the Redirect URI you specified in your app settings. The stored procedure returns the URL to the OAuth endpoint.
- Log in and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL.
Refresh the OAuth Access Token
You can set InitiateOAuth to REFRESH to automatically refresh the OAuth access token when it expires, or you can call the RefreshOAuthAccessToken stored procedure to refresh the token manually.
Automatic Refresh
To refresh the token with InitiateOAuth, set the following on the first data connection:
- OAuthAccessToken: Set this to the access token returned by GetOAuthAccessToken.
- InitiateOAuth: Set this to REFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthSettingsLocation: Set this to the path where the driver will save the OAuth values, to be persisted across connections.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- OAuthRefreshToken: Set this to the refresh token returned by GetOAuthAccessToken.
- ChannelId: Set this to the Id of a YouTube channel. If not specified, data is returned for the authenticated user's channel.
- ContentOwnerId: Set this if you want to generate content owner reports.
On subsequent data connections, set the following:
- InitiateOAuth
- OAuthSettingsLocation
- ChannelId: Set this to the Id of a YouTube channel. If not specified, data is returned for the authenticated user's channel.
- ContentOwnerId: Set this if you want to generate content owner reports.
Manual Refresh
You can use the RefreshOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken. Call the stored procedure after the ExpiresIn parameter value returned by GetOAuthAccessToken has elapsed. You need the following connection properties to be set:
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
You can then call RefreshOAuthAccessToken with the following parameter set:
- OAuthRefreshToken: Set this to the OAuthRefreshToken returned by GetOAuthAccessToken.
Service Accounts
Service accounts have silent authentication, without user authentication in the browser. You can also use a service account to delegate enterprise-wide access scopes to the driver.
You need to create an OAuth application in this flow. See Creating a Custom OAuth App to create and authorize an app. You can then connect to YouTube Analytics data that the service account has permission to access.
After setting the following connection properties, you are ready to connect:
- InitiateOAuth: Set this to GETANDREFRESH.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- OAuthJWTCertType: Set this to "PEMKEY_FILE".
- OAuthJWTCert: Set this to the path to the .pem file you generated.
- OAuthJWTCertPassword: Set this to the password of the .pem file.
- OAuthJWTCertSubject: Set this to "*" to pick the first certificate in the certificate store.
- OAuthJWTSubject: Set this to the email address of the user for whom the application is requesting delegate access. Note that delegate access must be granted by an administrator.
- ChannelId: Set this to the Id of a YouTube channel. If not specified, data is returned for the authenticated user's channel.
- ContentOwnerId: Set this if you want to generate content owner reports.
- Creates and signs the JWT with the claim set required by the driver.
- Exchanges the JWT for the access token.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
- Submits the JWT for a new access token when the token expires.