vCenter ReST API 身份验证
Posted
技术标签:
【中文标题】vCenter ReST API 身份验证【英文标题】:vCenter REST API authentication 【发布时间】:2017-12-15 00:34:39 【问题描述】:我正在关注https://code.vmware.com/apis/62/vcenter-management#/
上的 vmware 文档,在使用 ReST API 时要提供哪些标头来对 VCenter Server 进行身份验证?
【问题讨论】:
【参考方案1】:对于python:
import requests
# https://vdc-download.vmware.com/vmwb-repository/dcr-public/1cd28284-3b72-4885-9e31-d1c6d9e26686/71ef7304-a6c9-43b3-a3cd-868b2c236c81/doc/operations/com/vmware/vcenter/vm.list-operation.html
sess = requests.post("https://XXXXXXXX/rest/com/vmware/cis/session", auth=('USERNAME', 'PASSWORD'), verify=False)
session_id = sess.json()['value']
resp = requests.get("https://XXXXXXXX/rest/vcenter/vm", verify=False, headers=
"vmware-api-session-id": session_id
)
print(u"resp.text = %s" % str(resp.text))
【讨论】:
【参考方案2】:让我举例说明,例如,为了从 Vcenter 获取 VM 列表,您需要执行哪些操作。
首先,您需要向https://vcsa/rest/com/vmware/cis/session
发出 POST 请求以获取会话 ID。
然后,您使用对 https://vcsa/rest/vcenter/vm
的 GET 请求,并将 HTTP 标头 vmware-api-session-id
设置为先前获得的会话 ID。
以下是 php 中的一些示例代码:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user@vsphere.local' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false)
echo 'Curl Error: ' . curl_error($ch);
exit;
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
【讨论】:
会话请求返回 Null(在同一台机器上使用 curl 从命令行工作就好了)【参考方案3】:PowerShell:
$User="<username>"
$Pass="<password>"
$Auth=$User+":"+$Pass
$Encoded=[System.Text.Encoding]::UTF8.GetBytes($Auth)
$EncodedAuth=[System.Convert]::ToBase64String($Encoded)
$Headers = @"Authorization"="Basic $($EncodedAuth)"
$SecPass=ConvertTo-SecureString -String $Pass -AsPlainText -Force
$Cred=[System.Management.Automation.PSCredential]::new($User,$SecPass)
<# Uncomment this part if you don't have a valid trusted certificate
$strIDontCarePolicy=@"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class IDontCarePolicy : ICertificatePolicy
public IDontCarePolicy()
public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb) return true;
"@
Add-Type -TypeDefinition $strIDontCarePolicy -PassThru
[System.Net.ServicePointManager]::CertificatePolicy = New-Object IDontCarePolicy
#>
$initSession=Invoke-RestMethod -Uri "https://<vCenter Server>/rest/com/vmware/cis/session" -Method Post -Headers $Headers
$SessionID=$initSession.Value
【讨论】:
【参考方案4】:对于 .NET 客户端
//仅当您没有有效证书时忽略证书
var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
return true;
;
using (var client = new HttpClient(handler))
var values = new Dictionary<string, string>
;
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
$""username":"password"")));
var content = new FormUrlEncodedContent(values);
//var stringContent = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync("https://vcsa/rest/com/vmware/cis/session", content);
var responseString = await response.Content.ReadAsAsync<KeyValuePair<string, string>>();
client.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer", responseString.Value);
var vmRespone = await client.GetAsync("https://vcsa/rest/vcenter/vm");
【讨论】:
以上是关于vCenter ReST API 身份验证的主要内容,如果未能解决你的问题,请参考以下文章
登录时 WooCommerce REST API 身份验证失败