Using in SDKs

Using in SDKs

The SDKs have direct support for JWT and OAuth 2.0 authentication, yet in some cases your application might need to provide an Access Token to the SDK directly.

Python
from boxsdk import Client, OAuth2

oauth = OAuth2(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    access_token='ACCESS_TOKEN',
    refresh_token='REFRESH_TOKEN',
)

client = Client(oauth)

user = client.user().get()
print(f'User ID is {user.id}')
Java
BoxAPIConnection api = new BoxAPIConnection("YOUR-CLIENT-ID",
    "YOUR-CLIENT-SECRET", "YOUR-ACCESS-TOKEN", "YOUR-REFRESH-TOKEN");

Developer Token

Similarly, developer tokens can be used directly in SDKs without needing any further SDK configuration.

TypeScript Gen
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
const {
  BoxDeveloperTokenAuth,
} = require('box-typescript-sdk-gen/lib/box/developerTokenAuth.generated.js');

const auth = new BoxDeveloperTokenAuth({ token: 'DEVELOPER_TOKEN_GOES_HERE' });
const client = new BoxClient({ auth });

const me = await client.users.getUserMe();
console.log(`My user ID is ${me.id}`);
Python Gen
from box_sdk_gen import BoxClient, BoxDeveloperTokenAuth

auth = BoxDeveloperTokenAuth(token="DEVELOPER_TOKEN_GOES_HERE")
client = BoxClient(auth=auth)

me = client.users.get_user_me()
print(f"My user ID is {me.id}")
.NET Gen (Beta)
using Box.Sdk.Gen;

var auth = new BoxDeveloperTokenAuth(token: "DEVELOPER_TOKEN_GOES_HERE");
var client = new BoxClient(auth: auth));

var me = await client.Users.GetUserMeAsync();
Console.WriteLine($"My user ID is {me.Id}");
Python
from boxsdk import Client, OAuth2

auth = OAuth2(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    access_token='DEVELOPER_TOKEN_GOES_HERE',
)
client = Client(auth)
me = client.user().get()
print(f'My user ID is {me.id}')
.NET
var config = new BoxConfigBuilder("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET", new Uri("http://localhost")).Build();
var session = new OAuthSession("YOUR_DEVELOPER_TOKEN", "N/A", 3600, "bearer");
var client = new BoxClient(config, session);
Java
BoxAPIConnection api = new BoxAPIConnection("YOUR-DEVELOPER-TOKEN");
Node
var BoxSDK = require('box-node-sdk');
var sdk = new BoxSDK({
	clientID: 'YOUR-CLIENT-ID',
	clientSecret: 'YOUR-CLIENT_SECRET'
});

var client = sdk.getBasicClient('YOUR-DEVELOPER-TOKEN');

App Token

When using App Token authentication it is possible to directly pass the App Token to the SDKs.

Python
from boxsdk import Client, OAuth2

auth = OAuth2(
  client_id='YOUR_CLIENT_ID', 
  client_secret='', 
  access_token='APP_ACCESS_TOKEN_GOES_HERE'
)
client = Client(auth)
.NET
var config = new BoxConfigBuilder("YOUR_CLIENT_ID", "N/A", new Uri("http://localhost")).Build();
var session = new OAuthSession("YOUR_APP_TOKEN", "N/A", 3600, "bearer");
var client = new BoxClient(config, session);
Java
BoxTransactionalAPIConnection api = new BoxTransactionalAPIConnection("YOUR-ACCESS-TOKEN");
Node
var BoxSDK = require('box-node-sdk');
var sdk = new BoxSDK({
	clientID: 'YOUR-CLIENT-ID',
	clientSecret: 'unused'
});

var client = sdk.getBasicClient('YOUR-APP-TOKEN');