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

COMING SOON 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.

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

message

401

unauthorized

One of:

  • B2 has not been enabled for this account
  • User is in B2 suspend
  • Invalid authorization

401

missing_phone_number

Account is missing a mobile phone number. Please update account settings.

Sample Code

Code

curl https://api.backblazeb2.com/b2api/v1/b2_authorize_account -u "ACCOUNT_ID:APPLICATION_KEY"

Output

{
  "accountId": "YOUR_ACCOUNT_ID",
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "2_20150807002553_443e98bf57f978fa58c284f8_24d25d99772e3ba927778b39c9b0198f412d2163_acct",
  "downloadUrl": "https://f700.backblazeb2.com",
  "minimumPartSize": 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/v1/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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "2_20150807002553_443e98bf57f978fa58c284f8_24d25d99772e3ba927778b39c9b0198f412d2163_acct",
  "downloadUrl": "https://f700.backblazeb2.com",
  "minimumPartSize": 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/v1/b2_authorize_account',
    headers = headers
    )
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

print 'auth token:', response_data['authorizationToken']
print 'api url:', response_data['apiUrl']
print 'download url:', response_data['downloadUrl']
print 'minimum part size:', response_data['minimumPartSize']

Output

auth token: 2_20150812013854_a01f0090bb8e38e6c332e1cd_40106700acdf06d4fd4053d4ff4072da4e71c154_acct
api url: https://api001.backblaze.com
download url: https://f700.backblazeb2.com
minimum part size: 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/v1/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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "2_20150807002553_443e98bf57f978fa58c284f8_24d25d99772e3ba927778b39c9b0198f412d2163_acct",
  "downloadUrl": "https://f700.backblazeb2.com",
  "minimumPartSize": 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/v1/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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "2_20150807002553_443e98bf57f978fa58c284f8_24d25d99772e3ba927778b39c9b0198f412d2163_acct",
  "downloadUrl": "https://f700.backblazeb2.com",
  "minimumPartSize": 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/v1/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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "2_20150807002553_443e98bf57f978fa58c284f8_24d25d99772e3ba927778b39c9b0198f412d2163_acct",
  "downloadUrl": "https://f700.backblazeb2.com",
  "minimumPartSize": 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/v1/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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "apiUrl": "https://apiNNN.backblazeb2.com",
  "authorizationToken": "2_20150807002553_443e98bf57f978fa58c284f8_24d25d99772e3ba927778b39c9b0198f412d2163_acct",
  "downloadUrl": "https://f700.backblazeb2.com",
  "minimumPartSize": 100000000
}