2024/05/30 10:51:10
HTTP 基本认证
本文介绍如何进行 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
// 需要设置环境变量 AGORA_CUSTOMER_KEY
final String customerKey = System.getenv("AGORA_CUSTOMER_KEY");
// 客户密钥
// 需要设置环境变量 AGORA_CUSTOMER_SECRET
final String customerSecret = System.getenv("AGORA_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
// 需要设置环境变量 AGORA_CUSTOMER_KEY
customerKey := os.Getenv("AGORA_CUSTOMER_KEY")
// 客户密钥
// 需要设置环境变量 AGORA_CUSTOMER_SECRET
customerSecret := os.Getenv("AGORA_CUSTOMER_SECRET")
// 拼接客户 ID 和客户密钥并使用 base64 进行编码
plainCredentials := customerKey + ":" + customerSecret
base64Credentials := base64.StdEncoding.EncodeToString([]byte(plainCredentials))
// 创建 Authorization header
authHeader := "Basic " + base64Credentials
// 打印生成的认证头部
fmt.Println(authHeader)
}
PHP
<?php
// 客户 ID
// 需要设置环境变量 AGORA_CUSTOMER_KEY
$customerKey = getenv("AGORA_CUSTOMER_KEY");
// 客户密钥
// 需要设置环境变量 AGORA_CUSTOMER_SECRET
$customerSecret = getenv("AGORA_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
// 需要设置环境变量 AGORA_CUSTOMER_KEY
string customerKey = Environment.GetEnvironmentVariable("AGORA_CUSTOMER_KEY");
// 客户密钥
// 需要设置环境变量 AGORA_CUSTOMER_SECRET
string customerSecret = Environment.GetEnvironmentVariable("AGORA_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
// 需要设置环境变量 AGORA_CUSTOMER_KEY
const customerKey = process.env.AGORA_CUSTOMER_KEY;
// 客户密钥
// 需要设置环境变量 AGORA_CUSTOMER_SECRET
const customerSecret = process.env.AGORA_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
# 需要设置环境变量 AGORA_CUSTOMER_KEY
customer_key = os.environ.get("AGORA_CUSTOMER_KEY")
# 客户密钥
# 需要设置环境变量 AGORA_CUSTOMER_SECRET
customer_secret = os.environ.get("AGORA_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