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.

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

Normal application keys come from the b2_create_key call. When using one of them, the "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 key id and application key are combined into a string in the format "keyId: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.

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_request

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

401

unauthorized

The accountId/applicationKeyId and/or the applicationKey are wrong.

401

unsupported

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

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 "ACCOUNT_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
}

Code

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

String accountId = ""; // Obtained from your B2 account page.
String applicationKey = ""; // Obtained from your B2 account page.
HttpURLConnection connection = null;
String headerForAuthorizeAccount = "Basic " + Base64.getEncoder().encodeToString((accountId + ":" + 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
}

Code

import base64
import json
import urllib2

id_and_key = 'hexAccountId_value:accountKey_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
}

Code

import Foundation

let accountId = "<your account 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 = "\(accountId):\(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
}

Code

require 'json'
require 'net/http'

account_id = "ACCOUNT_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(account_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
}

Code

String accountId = "ACCOUNT_ID"; //B2 Cloud Storage Account 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(accountId + ":" + 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
}

Code

$account_id = ""; // Obtained from your B2 account page
$application_key = ""; // Obtained from your B2 account page
$credentials = base64_encode($account_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
}