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.

Request

Request HTTP Headers

Authorization

required

An HTTP basic auth value constructed as follows:

  • The B2 account id and B2 application key for the account are combined into a string in the format "accountId: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.

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.

Response Errors

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

status

code

message

401

unauthorized

B2 has not been enabled for this account

401

unauthorized

User is in B2 suspend

401

unauthorized

Cannot authorize domain site license account

401

unauthorized

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

var accountId = "ACCOUNT_ID" // Obtained from your B2 account page.
var applicationKey = "APPLICATION_KEY" // Obtained from your B2 account page.
if let url = NSURL(string: "https://api.backblazeb2.com/b2api/v1/b2_authorize_account") {    
    let base64Str = "\(accountId):\(applicationKey)".dataUsingEncoding(NSUTF8StringEncoding)!.base64EncodedStringWithOptions(.Encoding76CharacterLineLength)
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "GET"
    request.addValue("Basic \(base64Str)", forHTTPHeaderField: "Authorization"))
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration());
    let task = session.dataTaskWithRequest(request, completionHandler:{(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        if data != nil {
            let json = NSString(data: data!, encoding:NSUTF8StringEncoding)
            print(json)
        }
    })
    task.resume()
}

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
}