|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# Authentication |
| 6 | + |
| 7 | +This document provides detailed instructions on how to authenticate with the Tianji API, including obtaining, using, and managing API keys. |
| 8 | + |
| 9 | +## Authentication Method |
| 10 | + |
| 11 | +The Tianji API uses **Bearer Token** authentication. You need to include your API key in the HTTP header of each API request. |
| 12 | + |
| 13 | +### HTTP Header Format |
| 14 | + |
| 15 | +```http |
| 16 | +Authorization: Bearer YOUR_API_KEY |
| 17 | +``` |
| 18 | + |
| 19 | +## Obtaining API Keys |
| 20 | + |
| 21 | +1. Log in to your Tianji instance |
| 22 | +2. Click on your avatar in the top right corner |
| 23 | +4. Find the **API Keys** section |
| 24 | +5. Click the + button to create a new API key |
| 25 | +6. Name your API key and save it |
| 26 | + |
| 27 | +## API Key Management |
| 28 | + |
| 29 | +### View Existing Keys |
| 30 | + |
| 31 | +In the **API Keys** section, you can see: |
| 32 | +- API key name/description |
| 33 | +- Creation date |
| 34 | +- Last used time |
| 35 | +- Usage count statistics |
| 36 | + |
| 37 | +### Delete API Keys |
| 38 | + |
| 39 | +If you need to revoke an API key: |
| 40 | +1. Find the API key you want to delete |
| 41 | +2. Click the **Delete** button |
| 42 | +3. Confirm the deletion operation |
| 43 | + |
| 44 | +:::warning Note |
| 45 | +After deleting an API key, all applications using that key will no longer be able to access the API. |
| 46 | +::: |
| 47 | + |
| 48 | +## Using API Keys |
| 49 | + |
| 50 | +### cURL Example |
| 51 | + |
| 52 | +```bash |
| 53 | +curl -X GET "https://your-tianji-domain.com/open/global/config" \ |
| 54 | + -H "Authorization: Bearer <your_api_key_here>" \ |
| 55 | + -H "Content-Type: application/json" |
| 56 | +``` |
| 57 | + |
| 58 | +### JavaScript/Node.js Example |
| 59 | + |
| 60 | +```javascript |
| 61 | +const apiKey = '<your_api_key_here>'; |
| 62 | +const baseUrl = 'https://your-tianji-domain.com/open'; |
| 63 | + |
| 64 | +const headers = { |
| 65 | + 'Authorization': `Bearer ${apiKey}`, |
| 66 | + 'Content-Type': 'application/json' |
| 67 | +}; |
| 68 | + |
| 69 | +// Using fetch |
| 70 | +const response = await fetch(`${baseUrl}/global/config`, { |
| 71 | + method: 'GET', |
| 72 | + headers: headers |
| 73 | +}); |
| 74 | + |
| 75 | +// Using axios |
| 76 | +const axios = require('axios'); |
| 77 | +const response = await axios.get(`${baseUrl}/global/config`, { |
| 78 | + headers: headers |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +### Python Example |
| 83 | + |
| 84 | +```python |
| 85 | +import requests |
| 86 | + |
| 87 | +api_key = '<your_api_key_here>' |
| 88 | +base_url = 'https://your-tianji-domain.com/open' |
| 89 | + |
| 90 | +headers = { |
| 91 | + 'Authorization': f'Bearer {api_key}', |
| 92 | + 'Content-Type': 'application/json' |
| 93 | +} |
| 94 | + |
| 95 | +# Using requests library |
| 96 | +response = requests.get(f'{base_url}/global/config', headers=headers) |
| 97 | +data = response.json() |
| 98 | +``` |
| 99 | + |
| 100 | +### PHP Example |
| 101 | + |
| 102 | +```php |
| 103 | +<?php |
| 104 | +$apiKey = '<your_api_key_here>'; |
| 105 | +$baseUrl = 'https://your-tianji-domain.com/open'; |
| 106 | + |
| 107 | +$headers = [ |
| 108 | + 'Authorization: Bearer ' . $apiKey, |
| 109 | + 'Content-Type: application/json' |
| 110 | +]; |
| 111 | + |
| 112 | +$ch = curl_init(); |
| 113 | +curl_setopt($ch, CURLOPT_URL, $baseUrl . '/global/config'); |
| 114 | +curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 115 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 116 | + |
| 117 | +$response = curl_exec($ch); |
| 118 | +curl_close($ch); |
| 119 | + |
| 120 | +$data = json_decode($response, true); |
| 121 | +?> |
| 122 | +``` |
| 123 | + |
| 124 | +## Permissions and Scope |
| 125 | + |
| 126 | +### API Key Permissions |
| 127 | + |
| 128 | +API keys inherit all permissions of their creator, including: |
| 129 | +- Access to all data in the user's workspaces |
| 130 | +- Execute all operations the user has permission for |
| 131 | +- Manage resources created by that user |
| 132 | + |
| 133 | +### Workspace Access |
| 134 | + |
| 135 | +API keys can only access workspaces that the user belongs to. If you need to access multiple workspaces, ensure your user account has appropriate permissions for those workspaces. |
| 136 | + |
| 137 | +## Error Handling |
| 138 | + |
| 139 | +### Common Authentication Errors |
| 140 | + |
| 141 | +#### 401 Unauthorized |
| 142 | + |
| 143 | +```json |
| 144 | +{ |
| 145 | + "error": { |
| 146 | + "code": "UNAUTHORIZED", |
| 147 | + "message": "Authorization not provided" |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +**Causes**: |
| 153 | +- No Authorization header provided |
| 154 | +- Incorrect API key format |
| 155 | + |
| 156 | +#### 403 Forbidden |
| 157 | + |
| 158 | +```json |
| 159 | +{ |
| 160 | + "error": { |
| 161 | + "code": "FORBIDDEN", |
| 162 | + "message": "Insufficient access" |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +**Causes**: |
| 168 | +- Invalid or deleted API key |
| 169 | +- User doesn't have permission to access the requested resource |
| 170 | + |
| 171 | +### Debugging Authentication Issues |
| 172 | + |
| 173 | +1. **Check API key format**: Ensure you're using the `Bearer token_here` format |
| 174 | +2. **Verify key validity**: Confirm the key still exists in the Tianji interface |
| 175 | +3. **Check permissions**: Ensure the user account has permission to access the target resource |
| 176 | +4. **Test simple endpoints**: Start by testing public endpoints like `/global/config` |
0 commit comments