forked from docs/doc-exports
Reviewed-by: Gergo-Bence Lorincz <a200452876@noreply.gitea.eco.tsi-dev.otc-service.com> Co-authored-by: qiujiandong1 <qiujiandong1@huawei.com> Co-committed-by: qiujiandong1 <qiujiandong1@huawei.com>
117 lines
14 KiB
HTML
117 lines
14 KiB
HTML
<a name="ucs_api_0009"></a><a name="ucs_api_0009"></a>
|
|
|
|
<h1 class="topictitle1">Authentication</h1>
|
|
<div id="body0000001709808674"><p id="ucs_api_0009__p172493595163">Requests for calling an API can be authenticated using an AK/SK pair.</p>
|
|
<div class="section" id="ucs_api_0009__section1572110453579"><h4 class="sectiontitle">AK/SK Authentication</h4><p id="ucs_api_0009__en-us_topic_0121671869_p5607113161512">An AK/SK is used to verify the identity of a request sender. In AK/SK authentication, a signature needs to be obtained and then added to requests.</p>
|
|
<div class="note" id="ucs_api_0009__en-us_topic_0121671869_n9dcbfe6b240a43cfa48bb67f6e4b0126"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="ucs_api_0009__en-us_topic_0121671869_a9057158d9de6408fb6d7843353d9b3aa">AK: access key ID, which is a unique identifier used together with an SK to sign requests cryptographically.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_a6b8f717e84924432b9b93893ca4141c1">SK: secret access key, which is used together with an AK to sign requests cryptographically. It identifies a request sender and prevents the request from being modified.</p>
|
|
</div></div>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p5684143782614">The following demo shows how to sign a request and use an HTTP client to send an HTTPS request.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p11164312544">Download the demo at <a href="https://github.com/api-gate-way/SdkDemo" target="_blank" rel="noopener noreferrer">https://github.com/api-gate-way/SdkDemo</a>.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p358590112719">If you do not need the demo project, visit the following URL to download the API Gateway signing SDK:</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_en-us_topic_0091607401_p39335237104034">Request an API Gateway signing SDK from the enterprise administrator.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p1656604461617">Decompress the downloaded package to obtain a JAR file. Add the decompressed JAR file to the dependency path, as shown below.</p>
|
|
<div class="fignone" id="ucs_api_0009__en-us_topic_0121671869_fig919329144115"><span class="figcap"><b>Figure 1 </b>Adding the API Gateway signing SDK</span><br><span><img id="ucs_api_0009__en-us_topic_0121671869_image174116208156" src="en-us_image_0000002302431869.png"></span></div>
|
|
<ol id="ucs_api_0009__en-us_topic_0121671869_ol18692431181510"><li id="ucs_api_0009__en-us_topic_0121671869_li039995792416"><span>Generate an AK/SK. (If you already have an AK/SK file, skip this step and find it. Generally, the file name is <strong id="ucs_api_0009__b1762714714354">credentials.csv</strong>.)</span><p><ol type="a" id="ucs_api_0009__en-us_topic_0121671869_ol1041165715248"><li id="ucs_api_0009__en-us_topic_0121671869_li641635711249">Log in to the console.</li><li id="ucs_api_0009__en-us_topic_0121671869_li15426115710241">Click the username and select <strong id="ucs_api_0009__b132917152552229">My Credentials</strong> from the drop-down list.</li></ol><ol type="a" start="3" id="ucs_api_0009__en-us_topic_0121671869_ol4435175711246"><li id="ucs_api_0009__li68111402005">In the navigation tree on the left, click <strong id="ucs_api_0009__b175788981352229">Access Keys</strong>.</li><li id="ucs_api_0009__li28119401016">Click <strong id="ucs_api_0009__b99026263352229">Create Access Key</strong>. The <strong id="ucs_api_0009__b58848444352229">Create Access Key</strong> dialog box is displayed.</li><li id="ucs_api_0009__en-us_topic_0121671869_li20543057142412">Click <strong id="ucs_api_0009__b84170315252229">OK</strong> to download the AK/SK.<div class="note" id="ucs_api_0009__en-us_topic_0121671869_note35511957122417"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p class="text" id="ucs_api_0009__en-us_topic_0121671869_p3570155782420">Keep the AK/SK secure.</p>
|
|
</div></div>
|
|
</li></ol>
|
|
</p></li><li id="ucs_api_0009__en-us_topic_0121671869_li1115311720450"><span>Download and decompress the demo code.</span></li><li id="ucs_api_0009__en-us_topic_0121671869_li19564155663214"><a name="ucs_api_0009__en-us_topic_0121671869_li19564155663214"></a><a name="en-us_topic_0121671869_li19564155663214"></a><span>Import the demo project into Eclipse.</span><p><div class="fignone" id="ucs_api_0009__en-us_topic_0121671869_fig16546145205014"><span class="figcap"><b>Figure 2 </b>Selecting an existing project</span><br><span><img id="ucs_api_0009__en-us_topic_0121671869_image1474131114720" src="en-us_image_0000002302431877.png"></span></div>
|
|
<div class="fignone" id="ucs_api_0009__en-us_topic_0121671869_fig767232218519"><span class="figcap"><b>Figure 3 </b>Selecting the decompressed demo code</span><br><span><img id="ucs_api_0009__en-us_topic_0121671869_image13419842205018" src="en-us_image_0000002302518741.png"></span></div>
|
|
<div class="fignone" id="ucs_api_0009__en-us_topic_0121671869_fig159778103242"><span class="figcap"><b>Figure 4 </b>Example structure</span><br><span><img id="ucs_api_0009__en-us_topic_0121671869_image128044312389" src="en-us_image_0000002267861934.png"></span></div>
|
|
</p></li><li id="ucs_api_0009__en-us_topic_0121671869_li878093111517"><span>Sign the request.</span><p><p id="ucs_api_0009__en-us_topic_0121671869_p10940124322710">The request signing method is integrated in the JAR files imported in <a href="#ucs_api_0009__en-us_topic_0121671869_li19564155663214">3</a>. The request needs to be signed before it is sent. The signature will then be added as part of the HTTP header to the request.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p979243115158">The demo code is classified into three classes:</p>
|
|
<ul id="ucs_api_0009__en-us_topic_0121671869_ul46077243144336"><li id="ucs_api_0009__en-us_topic_0121671869_li373103144336"><strong id="ucs_api_0009__b4403940152229">AccessService</strong>: an abstract class that merges the GET, POST, PUT, and DELETE methods into the <strong id="ucs_api_0009__b130442200352229">access</strong> method</li><li id="ucs_api_0009__en-us_topic_0121671869_li15119103144339"><strong id="ucs_api_0009__b27801614852229">Demo</strong>: an execution entry used to simulate the sending of GET, POST, PUT, and DELETE requests</li><li id="ucs_api_0009__en-us_topic_0121671869_li54381430144341"><strong id="ucs_api_0009__b14755519752229">AccessServiceImpl</strong>: <strong id="ucs_api_0009__b110786280652229">access</strong> method implementation, which contains the code required for communication with API Gateway</li></ul>
|
|
<ol type="a" id="ucs_api_0009__en-us_topic_0121671869_ol1515318170459"><li id="ucs_api_0009__en-us_topic_0121671869_li1985133317503">Edit the main method in the <strong id="ucs_api_0009__b115127390052229">Demo.java</strong> file and replace the bold text with actual values.<p id="ucs_api_0009__en-us_topic_0121671869_p2061535616161">If you use other methods such as POST, PUT, and DELETE, see the corresponding comment.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p129401344175213">Specify <em id="ucs_api_0009__i195023873752229">region</em>, <em id="ucs_api_0009__i7055934252229">serviceName</em>, <em id="ucs_api_0009__i112148880452229">AK/SK</em>, and <em id="ucs_api_0009__i89114507352229">URL</em> with actual values. In this demo, the URLs for accessing VPC resources are used.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p27531945173720">To obtain the project ID in the URLs, see <a href="ucs_api_0018.html">Obtaining a Project ID</a>.</p>
|
|
<p id="ucs_api_0009__p164542183010">To obtain the endpoint, see "Regions and Endpoints".</p>
|
|
<div class="codecoloring" codetype="Java" id="ucs_api_0009__en-us_topic_0121671869_screen89816540225"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
|
|
<span class="normal"> 2</span>
|
|
<span class="normal"> 3</span>
|
|
<span class="normal"> 4</span>
|
|
<span class="normal"> 5</span>
|
|
<span class="normal"> 6</span>
|
|
<span class="normal"> 7</span>
|
|
<span class="normal"> 8</span>
|
|
<span class="normal"> 9</span>
|
|
<span class="normal">10</span>
|
|
<span class="normal">11</span>
|
|
<span class="normal">12</span>
|
|
<span class="normal">13</span>
|
|
<span class="normal">14</span>
|
|
<span class="normal">15</span>
|
|
<span class="normal">16</span>
|
|
<span class="normal">17</span>
|
|
<span class="normal">18</span>
|
|
<span class="normal">19</span>
|
|
<span class="normal">20</span>
|
|
<span class="normal">21</span>
|
|
<span class="normal">22</span>
|
|
<span class="normal">23</span>
|
|
<span class="normal">24</span>
|
|
<span class="normal">25</span>
|
|
<span class="normal">26</span>
|
|
<span class="normal">27</span>
|
|
<span class="normal">28</span>
|
|
<span class="normal">29</span>
|
|
<span class="normal">30</span>
|
|
<span class="normal">31</span>
|
|
<span class="normal">32</span>
|
|
<span class="normal">33</span>
|
|
<span class="normal">34</span>
|
|
<span class="normal">35</span>
|
|
<span class="normal">36</span>
|
|
<span class="normal">37</span>
|
|
<span class="normal">38</span></pre></div></td><td class="code"><div><pre><span></span><span class="c1">//TODO: Replace region with the name of the region in which the service to be accessed is located. </span>
|
|
<span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">region</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">""</span><span class="p">;</span>
|
|
|
|
<span class="c1">//TODO: Replace vpc with the name of the service you want to access. For example, ecs, vpc, iam, and elb.</span>
|
|
<span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serviceName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">""</span><span class="p">;</span>
|
|
|
|
<span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">UnsupportedEncodingException</span>
|
|
<span class="p">{</span>
|
|
<span class="c1">//TODO: Replace the AK and SK with those obtained on the My Credentials page.</span>
|
|
<span class="n">String</span><span class="w"> </span><span class="n">ak</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"ZIRRKMTWP******1WKNKB"</span><span class="p">;</span>
|
|
<span class="n">String</span><span class="w"> </span><span class="n">sk</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"Us0mdMNHk******YrRCnW0ecfzl"</span><span class="p">;</span>
|
|
|
|
<span class="c1">//TODO: To specify a project ID (multi-project scenarios), add the X-Project-Id header.</span>
|
|
<span class="c1">//TODO: To access a global service, such as IAM, DNS, CDN, and TMS, add the X-Domain-Id header to specify an account ID.</span>
|
|
<span class="c1">//TODO: To add a header, find "Add special headers" in the AccessServiceImple.java file.</span>
|
|
|
|
<span class="c1">//TODO: Test the API</span>
|
|
<span class="n">String</span><span class="w"> </span><span class="n">url</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"https://{Endpoint}/v1/{project_id}/vpcs"</span><span class="p">;</span>
|
|
<span class="n">get</span><span class="p">(</span><span class="n">ak</span><span class="p">,</span><span class="w"> </span><span class="n">sk</span><span class="p">,</span><span class="w"> </span><span class="n">url</span><span class="p">);</span>
|
|
|
|
<span class="c1">//TODO: When creating a VPC, replace {project_id} in postUrl with the actual value.</span>
|
|
<span class="c1">//String postUrl = "https://serviceEndpoint/v1/{project_id}/cloudservers";</span>
|
|
<span class="c1">//String postbody ="{\"vpc\": {\"name\": \"vpc\",\"cidr\": \"192.168.0.0/16\"}}";</span>
|
|
<span class="c1">//post(ak, sk, postUrl, postbody);</span>
|
|
|
|
<span class="c1">//TODO: When querying a VPC, replace {project_id} in url with the actual value.</span>
|
|
<span class="c1">//String url = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}";</span>
|
|
<span class="c1">//get(ak, sk, url);</span>
|
|
|
|
<span class="c1">//TODO: When updating a VPC, replace {project_id} and {vpc_id} in putUrl with the actual values.</span>
|
|
<span class="c1">//String putUrl = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}";</span>
|
|
<span class="c1">//String putbody ="{\"vpc\":{\"name\": \"vpc1\",\"cidr\": \"192.168.0.0/16\"}}";</span>
|
|
<span class="c1">//put(ak, sk, putUrl, putbody);</span>
|
|
|
|
<span class="c1">//TODO: When deleting a VPC, replace {project_id} and {vpc_id} in deleteUrl with the actual values.</span>
|
|
<span class="c1">//String deleteUrl = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}";</span>
|
|
<span class="c1">//delete(ak, sk, deleteUrl);</span>
|
|
<span class="p">}</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
</li><li id="ucs_api_0009__en-us_topic_0121671869_li18412306320">Compile and run the code to call an API.<p id="ucs_api_0009__en-us_topic_0121671869_p188281527115415"><a name="ucs_api_0009__en-us_topic_0121671869_li18412306320"></a><a name="en-us_topic_0121671869_li18412306320"></a>In the <strong id="ucs_api_0009__b159564331552229">Package Explorer</strong> area on the left, right-click <strong id="ucs_api_0009__b89001793052229">Demo.java</strong> and choose <strong id="ucs_api_0009__b170607715052229">Run AS</strong> > <strong id="ucs_api_0009__b43983792252229">Java Application</strong> from the shortcut menu to run the demo code.</p>
|
|
<p id="ucs_api_0009__en-us_topic_0121671869_p20946560369">You can view the API calling logs on the console.</p>
|
|
</li></ol>
|
|
</p></li></ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="ucs_api_0007.html">Calling APIs</a></div>
|
|
</div>
|
|
</div>
|
|
|