# 实现 HTTP 基本认证 (/zh-CN/realtime-media/media-push/build/enable-media-push/http-basic-auth)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

## 前提条件 [#前提条件]

本文介绍如何进行 HTTP 基本认证。

已经从声网控制台获取客户 ID 和客户密钥，详见[开通服务](/zh-CN/realtime-media/media-push/build/enable-media-push/enable-service)。

## 生成方法 [#生成方法]

进行 HTTP 基本认证时，你需要使用声网提供的客户 ID 和客户密钥生成一个 Base64 算法编码的凭证，并填入 HTTP 请求头部的 `Authorization` 字段中。本节介绍如何获取用于 HTTP 基本认证的 `Authorization` 字段。

### 使用三方工具生成 [#使用三方工具生成]

你可以使用[第三方在线工具](https://www.debugbear.com/basic-auth-header-generator)快速得到 Authorization 值。将客户 ID 和客户密钥分别填入 Username 和 Password 框，得到形如 `Authorization: Basic NDI1OTQ3N2I4MzYy...YwZjA=` 的结果。冒号后的内容即是 `Authorization` 值。

### 使用示例代码生成 [#使用示例代码生成]

复制如下代码，填入你的客户 ID 和客户密钥，即可生成对应的 `Authorization` 字段：

```java tab="Java" tabGroup="operating-systems"
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 tab="Golang" tabGroup="operating-systems"
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 tab="PHP" tabGroup="operating-systems"
<?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;
```

```cs tab="C#" tabGroup="operating-systems"
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 tab="Node.js" tabGroup="operating-systems"
// 基于 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 tab="Python" tabGroup="operating-systems"
# -- 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
```
