Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Signature and call API by CloudStack

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how CloudStack generates Signature to call API, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

One: preface

/ * this paragraph-Mandarin * /

CloudStack API is also based on Web Service and can be written in any language that supports HTTP calls, such as Java,PHP.

The calling code (caller) first needs to be authenticated at the management server. Currently, CloudStack uses two authentication methods:

Session certification: through login API, get a JSESSIONID cookie and a SESSIONKEY token.

API Key certification.

/ * this paragraph-Mandarin * /

Second, using API Key for authentication:

1. Before generating Signature, we must first have [API Key] & [Security Key]. This article mainly records the implementation of Signature encryption. How to generate these two Key hosts will not be discussed in detail.

2. The official language for generating Signature is Perl: the address is as follows, you can watch for yourself:

Https://github.com/snumano/CloudStack-API/blob/master/generate-url.pl

3. Then the owner found the Python version: the address is as follows:

Https://github.com/jasonhancock/cloudstack-pythonclient/blob/master/CloudStack/BaseClient.py

4. Of course, it can be found in the Java version: CloudStack open source package. Here the host shares a post:

Http://blog.csdn.net/rishengcsdn/article/details/38387721

Third: use C# to generate Signature. With the above information, it is easy to write the C# version. Of course, there are some differences. There are several problems that need to be paid attention to:

1. The [ApiKey] & [SecretKey] string is long, so don't make any mistakes.

2. Parameters are encrypted in a smaller form

3. When the parameters are encrypted, they need to be sorted (based on the first letter of the parameters)

4. Unified coding before encryption: UTF8

5. Encryption method: HmacSha1

6. After encryption, the message confirmation code is: UTF8.

7. Combine strings

Four: code implementation (for reference only, test code)

For example, get the status information of the CVM

Parameter information:

Call address: "http://*.*.*.*:**/client/api?""

API name: "listVirtualMachines"

Parameters:

Details=stats statu

Account=admin operator account

Id=**** virtual host number

API KEY: "*"

SecretKey: "*"

/ / generate CloudStack Signature executable Url / / generate CloudStack executable Url / public string GetSignatureApiUrl () {/ * call CloudStack API to generate Signature and combine complete URL * 1, obtain parameters * 2 according to configuration, validity verification * 3, Parameter information sorting * 4, unified coding UTF-8 * 5, using HmacSha1 encoding encryption * 6, encrypted content again to UTF-8 * 7, combination * / var ApiKey= "*" Var SecretKey= "*"; / / CloudStack API call address var DeveloperServer = "http://*.*.*.*:**/client/api?"; / / ApiKey uniform coding Encoding enc = new UTF8Encoding (); string encodedApiKey = HttpUtility.UrlEncode (ApiKey, enc) String encodedSecreKey = HttpUtility.UrlEncode (SecretKey, enc); / / Parameter, passed in List Args = new List (); Args.Add ("command=" + Command) Args.Add ("details=stats"); Args.Add ("account=admin"); Args.Add ("id=****") / / validation / / slightly var oldparmsString=string.Join ("&", Args); Args.Add ("apikey=" + ApiKey) / / sort the parameter information Args = Args.OrderBy (o = > o) .ToList () / / Parameter concatenation var paramsString = string.Join ("&", Args); / / encrypt the execution message string signature = new HashEncryptHelp () .HmacSha1 (encodedSecreKey, paramsString.Tolower ()); / / transfer the encrypted message to UTF-8 string encodedSignature = HttpUtility.UrlEncode (signature, enc) / / combine URL characters string url = DeveloperServer + oldparmsString+ "& apikey=" + encodedApiKey + "& signature=" + encodedSignature; return url;}

Output: http://*.*.*.*:**/client/api?command=listVirtualMachines&details=stats&account=admin&id=****&apikey=****&signature=****

Test execution: successful.

Thank you for reading this article carefully. I hope the article "how to generate Signature and call API" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report