b2_authorize_account

Used to log in to the B2 API. Returns an authorization token that can be used for account-level operations, and a URL that should be used as the base URL for subsequent API calls.

You can use either the master application key or a normal application key.

NOTE: the account ID can be used in place of the master application key ID.

You'll find the master application key on the B2 Cloud Storage Buckets page on the web site. When using the master application key, use your "master application key ID" and the "application key" you got from the site.

Master Application Key: This is the first key you have access to, it is available on the web application. This key has all capabilities, access to all buckets, and has no file prefix restrictions or expiration.

Application Key(s) [non-master]: These are other keys created by you and can be limited to a bucket, with a specific file prefix and can expire.

Normal application keys come from the b2_create_key call. When using one of them, the "application key ID" and "application key" are the ones returned when you created the key.

Request

Request HTTP Headers

Authorization

required

An HTTP basic auth value constructed as follows:

  • The application key id and application key are combined into a string in the format "applicationKeyId:applicationKey".
  • The combined string is Base64 encoded.
  • "Basic " is put before the encoded string.

Request HTTP Message Body Parameters

This request has no parameters. The Authorization header is all that is needed.

Response

Response HTTP Status 200

Authorization succeeded. The JSON response will contain:

accountId

The identifier for the account.

authorizationToken

An authorization token to use with all calls, other than b2_authorize_account, that need an Authorization header. This authorization token is valid for at most 24 hours.

allowed

An object (see below) containing the capabilities of this auth token, and any restrictions on using it.

apiUrl

The base URL to use for all API calls except for uploading and downloading files.

downloadUrl

The base URL to use for downloading files.

recommendedPartSize

The recommended size for each part of a large file. We recommend using this part size for optimal upload performance.

For very large files, you may need to use a larger size to stay within the limit of 10,000 parts per large file.

Currently, the value returned is always 100,000,000, but we recommend that you write your code to get the number here, rather than use a hard-coded constant. See Large Files.

absoluteMinimumPartSize

The smallest possible size of a part of a large file (except the last one). This is smaller than the recommendedPartSize. If you use it, you may find that it takes longer overall to upload a large file.

minimumPartSize

DEPRECATED: This field will always have the same value as recommendedPartSize. Use recommendedPartSize instead.

s3ApiUrl

The base URL to use for all API calls using the S3 compatible API.

The allowed field contains information about what you're allowed to do with this auth token, which is based on the capabilities of the application key in the request.

capabilities

required

A list of strings, each one naming a capability the key has. Possibilities are: listKeys, writeKeys, deleteKeys, listBuckets, writeBuckets, deleteBuckets, listFiles, readFiles, shareFiles, writeFiles, and deleteFiles.

bucketId

optional

When present, access is restricted to one bucket.

bucketName

optional

When bucketId is set, and it is a valid bucket that has not been deleted, this field is set to the name of the bucket. It's possible that bucketId is set to a bucket that no longer exists, in which case this field will be null. It's also null when bucketId is null.

namePrefix

optional

When present, access is restricted to files whose names start with the prefix

Response Errors

Authorization Failed. If possible the server will return a JSON error structure. Errors include:

status

code

description

400

bad_bucket_id

The requested bucket ID does not match an existing bucket.

400

bad_request

The request had the wrong fields or illegal values. The message returned with the error will describe the problem.

400

cannot_delete_non_empty_bucket

A bucket must be empty before it can be deleted. To delete this bucket, first remove all of the files in the bucket, then try the delete operation again.

401

unauthorized

The applicationKeyId and/or the applicationKey are wrong.

401

unsupported

The applicationKeyId is valid, but cannot be used with this version of the B2 API. The message contains information about what the problem is.

403

transaction_cap_exceeded

Transaction cap exceeded. To increase your cap, sign in to your B2 Cloud Storage account online. Then select the Caps & Alerts link in the B2 Cloud Storage section of the sidebar.

API Versions

v2: Return bucket name (Sept 13, 2018)

When using an application key with a bucket restriction, the name of the bucket is now returned.

Sample Code

Code

curl https://api.backblazeb2.com/b2api/v2/b2_authorize_account -u "APPLICATION_KEY_ID:APPLICATION_KEY"

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

Code

import java.io.*;
import java.util.Base64;
import java.net.HttpURLConnection;
import java.net.URL;

String applicationKeyId = ""; // Obtained from your B2 account page.
String applicationKey = ""; // Obtained from your B2 account page.
HttpURLConnection connection = null;
String headerForAuthorizeAccount = "Basic " + Base64.getEncoder().encodeToString((applicationKeyId + ":" + applicationKey).getBytes());
try {
    URL url = new URL("https://api.backblazeb2.com/b2api/v2/b2_authorize_account");
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Authorization", headerForAuthorizeAccount);
    InputStream in = new BufferedInputStream(connection.getInputStream());    
    String jsonResponse = myInputStreamReader(in);
    System.out.println(jsonResponse);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
}

static public String myInputStreamReader(InputStream in) throws IOException {
    InputStreamReader reader = new InputStreamReader(in);
    StringBuilder sb = new StringBuilder();
    int c = reader.read();
    while (c != -1) {
        sb.append((char)c);
        c = reader.read();
    }
    reader.close();
    return sb.toString();
}

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

Code

import base64
import json
import urllib2

id_and_key = 'applicationKeyId_value:applicationKey_value'
basic_auth_string = 'Basic ' + base64.b64encode(id_and_key)
headers = { 'Authorization': basic_auth_string }

request = urllib2.Request(
    'https://api.backblazeb2.com/b2api/v2/b2_authorize_account',
    headers = headers
    )
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

Code

import Foundation

let applicationKeyId = "<your application key id>" // Obtained from your B2 account page.
let applicationKey = "<your application key>" // Obtained from your B2 account page.

// Create a url and authorization header data
guard let url = URL(string: "https://api.backblazeb2.com/b2api/v2/b2_authorize_account"),
	let authNData = "\(applicationKeyId):\(applicationKey)".data(using: .utf8) else {
	return
}

// Create the request. Take note that the authNData must be base64 encoded.
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Basic \(authNData.base64EncodedString())", forHTTPHeaderField: "Authorization")

// Create a  task with the request we created above.
let task = URLSession.shared.dataTask(with: request) { (data, response, error ) in
	if data != nil {
		let json = String(data: data!, encoding:.utf8)
		print(json!)
	}
}

// Start the task
task.resume()

// Swift 4.1 (swiftlang-902.0.48 clang-902.0.37.1) Xcode 9.3.1 (9E501)

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

Code

require 'json'
require 'net/http'

application_key_id = "APPLICATION_KEY_ID" # Obtained from your B2 account page.
application_key = "APPLICATION_KEY" # Obtained from your B2 account page.
uri = URI("https://api.backblazeb2.com/b2api/v2/b2_authorize_account")
req = Net::HTTP::Get.new(uri)	
req.basic_auth(application_key_id, application_key)
http = Net::HTTP.new(req.uri.host, req.uri.port)
http.use_ssl = true
res = http.start {|http| http.request(req)}
case res
when Net::HTTPSuccess then
    json = res.body
when Net::HTTPRedirection then
    fetch(res['location'], limit - 1)
else
    res.error!
end

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

Code

String applicationKeyId = "ACCOUNT_ID"; //B2 Cloud Storage Application Key ID
String applicationKey = "APPLICATION_KEY"; //B2 Cloud Storage Application Key
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.backblazeb2.com/b2api/v2/b2_authorize_account");
String credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(applicationKeyId + ":" + applicationKey));
webRequest.Headers.Add("Authorization", "Basic " + credentials);
webRequest.ContentType = "application/json; charset=utf-8";
WebResponse response = (HttpWebResponse)webRequest.GetResponse();
String json = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Console.WriteLine(json);

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}

Code

$application_key_id = ""; // Obtained from your B2 account page
$application_key = ""; // Obtained from your B2 account page
$credentials = base64_encode($application_key_id . ":" . $application_key);
$url = "https://api.backblazeb2.com/b2api/v2/b2_authorize_account";

$session = curl_init($url);

// Add headers
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Authorization: Basic " . $credentials;
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);  // Add headers

curl_setopt($session, CURLOPT_HTTPGET, true);  // HTTP GET
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Receive server response
$server_output = curl_exec($session);
curl_close ($session);
echo ($server_output);

Output

{
  "absoluteMinimumPartSize": 5000000,
  "accountId": "YOUR_ACCOUNT_ID",
  "allowed": {
    "bucketId": "BUCKET_ID",
    "bucketName": "BUCKET_NAME",
    "capabilities": [
      "listBuckets",
      "listFiles",
      "readFiles",
      "shareFiles",
      "writeFiles",
      "deleteFiles"
    ],
    "namePrefix": null
  },
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "4_0022623512fc8f80000000001_0186e431_d18d02_acct_tH7VW03boebOXayIc43-sxptpfA=",
  "downloadUrl": "https://f002.backblazeb2.com",
  "recommendedPartSize": 100000000,
  "s3ApiUrl": "https://s3.us-west-NNN.backblazeb2.com"
}