实现 HTTP 基本认证
声网 RESTful API 使用 HTTP 基本认证进行鉴权。在调用 RESTful API 之前,你需要实现 HTTP 基本认证。
前提条件
已经从声网控制台获取客户 ID 和客户密钥,详见开通服务。
实现方法
进行 HTTP 基本认证时,你需要使用声网提供的客户 ID 和客户密钥生成一个 Base64 算法编码的凭证,并填入 HTTP 请求头部的 Authorization
字段中。
复制如下代码,填入你的客户 ID 和客户密钥,即可生成对应的 Authorization
字段:
- Java
- Golang
- PHP
- C#
- node.js
- Python
Java
import java.io.IOException;
import java.net.URI;
import java.util.Base64;
public class Base64Encoding {
public static void main(String[] args) throws IOException, InterruptedException {
// 客户 ID
final String customerKey = "Your customer key";
// 客户密钥
final String customerSecret = "Your customer secret";
// 拼接客户 ID 和客户密钥并使用 base64 编码
String plainCredentials = customerKey + ":" + customerSecret;
String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
// 创建 authorization header
String authorizationHeader = "Basic " + base64Credentials;
}
}
Go
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
"encoding/base64"
)
func main() {
// 客户 ID
customerKey := "Your customer key"
// 客户密钥
customerSecret := "Your customer secret"
// 拼接客户 ID 和客户密钥并使用 base64 进行编码
plainCredentials := customerKey + ":" + customerSecret
base64Credentials := base64.StdEncoding.EncodeToString([]byte(plainCredentials))
// 创建 Authorization header
authHeader := "Basic " + base64Credentials
// 打印生成的认证头部
fmt.Println(authHeader)
}
PHP
<?php
// 客户 ID
$customerKey = "Your customer key";
// 客户密钥
$customerSecret = "Your customer secret";
// 拼接客户 ID 和客户密钥
$credentials = $customerKey . ":" . $customerSecret;
// 使用 base64 进行编码
$base64Credentials = base64_encode($credentials);
// 创建 authorization header
$arr_header = "Authorization: Basic " . $base64Credentials;
C#
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestPostExample
{
public static void Main()
{
// 客户 ID
string customerKey = "Your customer key";
// 客户密钥
string customerSecret = "Your customer secret";
// 拼接客户 ID 和客户密钥
string plainCredential = customerKey + ":" + customerSecret;
// 使用 base64 进行编码
var plainTextBytes = Encoding.UTF8.GetBytes(plainCredential);
string encodedCredential = Convert.ToBase64String(plainTextBytes);
// 创建 authorization header
string authorizationHeader = "Authorization: Basic " + encodedCredential;
}
}
}
JavaScript
// 基于 node.js 实现的 HTTP 基本认证示例
const https = require('https')
// 客户 ID
const customerKey = "Your customer key"
// 客户密钥
const customerSecret = "Your customer secret"
// 拼接客户 ID 和客户密钥
const plainCredential = customerKey + ":" + customerSecret
// 使用 base64 进行编码
encodedCredential = Buffer.from(plainCredential).toString('base64')
// 创建 authorization header
authorizationField = "Basic " + encodedCredential
Python
# -- coding utf-8 --
# Python 3
import base64
import http.client
# 客户 ID
customer_key = "Your customer key"
# 客户密钥
customer_secret = "Your customer secret"
# 拼接客户 ID 和客户密钥
credentials = customer_key + ":" + customer_secret
# 使用 base64 进行编码
base64_credentials = base64.b64encode(credentials.encode("utf8"))
credential = base64_credentials.decode("utf8")
# 创建 authorization header
basic_auth_header = 'basic ' + credential