Consumer Credentials
To authenticate as a Consumer in the API, you'll need some credentials associated with the Domain you intend to operate within.
- The base URL for the instance your Domain was assigned to
- A set of valid Client Credentials as a Base64 Authorization Header.
- A set of valid Consumer credentials (email and password)
import Bitcapital from 'bitcapital-core-sdk';
// Initialize the session instance to authenticate
// using the Bitcapital Core OAuth 2.0 provider.
const bitcapital = Bitcapital.initialize({
// Instance URL for REST API calls
baseURL: 'https://your-instance.btcore.app',
// Credentials for OAuth 2.0 requests
clientId: '< YOUR CLIENT_ID HERE >',
clientSecret: '< YOUR CLIENT_SECRET HERE >',
});
try {
// Authenticate a user with email and password from Bitcapital Core
// If succeeds and available, the credentials will be stored in the
// session instance and in the local storage (for browser environments).
const user = await bitcapital.session().password({
email: "[email protected]",
password: "sample_password_123",
});
// The session returns the consumer user info and its credentials
console.log(user.credentials.accessToken);
// To logout and clear the current credentials, use the "destroy" action
await bitcapital.session().destroy();
} catch(exception) {
// Something went wrong, probably credentials are invalid
console.error(exception);
}
curl \
-H "Authorization: Basic $(echo \"CLIENT_ID:CLIENT_SECRET\" | base64)" \
--data "grant_type=password&[email protected]&password=sample_password123" \
https://instance-url.btcore.app/oauth/token
public class BitcapitalClient {
public static class OAuthTokenResponse {
public String access_token;
public String refreshToken;
public String expiresIn;
public String userId;
}
/**
* Uses Retrofit2 annotations for HTTP request mapping.
*/
public static interface OAuthWebService {
@FormUrlEncoded
@POST("oauth/token")
Call<OAuthTokenResponse> token(
@Header("Authorization") String basic,
@Field("username") String email,
@Field("password") String password,
@Field("scopes") String [] scopes,
@Field("grant_type") String grantType
);
}
/**
* Executes an OAuth 2.0 authentication using OkHttp3 and Retrofit2.
*/
public static Call<> execute() {
Retrofit retrofit = new Retrofit.Builder()
// Add your instance URL
.baseUrl("https://testnet.btcore.app")
// Uses GSON for serialization
.addConverterFactory(GsonConverterFactory.create())
.build();
// Initialize the oauth web service interface
OAuthWebService oauthWebService = retrofit.create(OAuthWebService.class);
// TODO: Put your params in the request call
return oauthWebService.token(...params).execute().body()
}
}
For more information, checkout de OAuth 2.0 API Reference.
Updated over 3 years ago