doc-exports/docs/modelarts/api-ref/modelarts_03_0004.html
Lai, Weijian 2f0818cf3d ModelArts API 22.3.0 version-20240311
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Lai, Weijian <laiweijian4@huawei.com>
Co-committed-by: Lai, Weijian <laiweijian4@huawei.com>
2024-04-05 09:35:42 +00:00

114 lines
15 KiB
HTML

<a name="modelarts_03_0004"></a><a name="modelarts_03_0004"></a>
<h1 class="topictitle1">Authentication</h1>
<div id="body1536114969127"><div class="p" id="modelarts_03_0004__en-us_topic_0094388456_p34423277184242">Requests for calling an API can be authenticated using either of the following methods: AK/SK-based authentication: Requests are authenticated by encrypting the request body using an AK/SK pair.<ul id="modelarts_03_0004__en-us_topic_0094388456_ul41374043184242"><li id="modelarts_03_0004__en-us_topic_0094388456_li36822067184242">Token-based authentication: Requests are authenticated using a token.</li><li id="modelarts_03_0004__en-us_topic_0094388456_li62963152184242">AK/SK authentication: Requests are encrypted using the access key ID (AK) and secret access key (SK).</li></ul>
</div>
<div class="section" id="modelarts_03_0004__en-us_topic_0170917208_en-us_topic_0168405764_section2417768214391"><h4 class="sectiontitle">Token-based Authentication</h4><div class="note" id="modelarts_03_0004__en-us_topic_0170917208_en-us_topic_0168405764_note133321911123"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="modelarts_03_0004__en-us_topic_0170917208_en-us_topic_0168405764_p1254514271026">The validity period of a token is 24 hours. When using a token for authentication, cache it to prevent frequently calling the IAM API used to obtain a user token.</p>
</div></div>
<p id="modelarts_03_0004__en-us_topic_0170917208_en-us_topic_0168405764_p18703477214">A token specifies temporary permissions in a computer system. During API authentication using a token, the token is added to requests to get permissions for calling the API.</p>
<p id="modelarts_03_0004__p4938113918117">In <a href="modelarts_03_0005.html">Making an API Request</a>, the process of calling the API used to obtain a user token is described. </p>
<pre class="screen" id="modelarts_03_0004__screen84049561000">{
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "<em id="modelarts_03_0004__i10299121512419">user_name</em>",
"password": "<em id="modelarts_03_0004__i175561812414">user_password</em>",
"domain": {
"name": "<em id="modelarts_03_0004__i1258621154113">domain_name</em>"
}
}
}
},
"scope": {
"project": {
"name": "<em id="modelarts_03_0004__i032612424111">project_name</em>"
}
}
}
}</pre>
<p id="modelarts_03_0004__p175512042712">After a token is obtained, the <strong id="modelarts_03_0004__b890013053514">X-Auth-Token</strong> header field must be added to requests to specify the token when calling other APIs. For example, if the token is <strong id="modelarts_03_0004__b20226313173613">ABCDEFJ....</strong>, <strong id="modelarts_03_0004__b9227121343610">X-Auth-Token: ABCDEFJ....</strong> can be added to a request as follows:</p>
<pre class="screen" id="modelarts_03_0004__en-us_topic_0170917208_en-us_topic_0168405764_screen11189101154015">POST https://<em id="modelarts_03_0004__i9375175113450">{endpoint}</em>/v1/<em id="modelarts_03_0004__i15733621175217">{project_id}</em>/services
Content-Type: application/json
<strong id="modelarts_03_0004__en-us_topic_0170917208_en-us_topic_0168405764_b172853338430">X-Auth-Token: ABCDEFJ....</strong></pre>
</div>
<div class="section" id="modelarts_03_0004__section7694258719"><h4 class="sectiontitle">AK/SK-based Authentication</h4><p id="modelarts_03_0004__en-us_topic_0094388456_p38992551184055">An AK/SK is used to verify the identity of a request sender. In AK/SK-based authentication, a signature needs to be obtained and then added to requests.</p>
<div class="note" id="modelarts_03_0004__en-us_topic_0094388456_note15388641184055"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p class="text" id="modelarts_03_0004__en-us_topic_0094388456_p4280047184055">AK: access key ID, which is a unique identifier used in conjunction with a secret access key to sign requests cryptographically.</p>
<p class="text" id="modelarts_03_0004__en-us_topic_0094388456_p38520424184055">SK: secret access key used in conjunction with an AK to sign requests cryptographically. It identifies a request sender and prevents the request from being modified.</p>
</div></div>
<p id="modelarts_03_0004__p1869494712339">The following uses a demo project to show how to sign a request and use an HTTP client to send an HTTPS request.</p>
<p id="modelarts_03_0004__p5694174711333">Download the demo project at <a href="https://github.com/api-gate-way/SdkDemo" target="_blank" rel="noopener noreferrer">https://github.com/api-gate-way/SdkDemo</a>.</p>
<div class="note" id="modelarts_03_0004__note1390973912213"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="modelarts_03_0004__p890914393214">ModelArts is a regional service. Specify the project ID when calling the API. Therefore, before running the demo project, add the project ID (<strong id="modelarts_03_0004__b1351724624011">X-Project-Id</strong>) in line 110 of access() in <strong id="modelarts_03_0004__b57901016154015">AccessServicelmpl.java</strong>. For details about how to obtain a project ID, see <a href="modelarts_03_0147.html">Obtaining a Project ID and Name</a>.</p>
</div></div>
<p id="modelarts_03_0004__p1694154793314">If you do not need the demo project, visit the following URL to download the API Gateway signing SDK:</p>
<p id="modelarts_03_0004__p19866332181318">Obtain the API Gateway signing SDK from the enterprise administrator.</p>
<p id="modelarts_03_0004__p146941947123310">Decompress the downloaded package and reference the obtained JAR files as dependencies, as highlighted in the following figure.</p>
<div class="fignone" id="modelarts_03_0004__fig1682017613919"><span class="figcap"><b>Figure 1 </b>JAR Files</span><br><span><img id="modelarts_03_0004__image2820066396" src="en-us_image_0000001862721045.png"></span></div>
<ol id="modelarts_03_0004__en-us_topic_0094388456_ol11139503184055"><li id="modelarts_03_0004__en-us_topic_0094388456_li33146668184055"><span>Generate an AK/SK. (If an AK/SK file has already been obtained, skip this step and locate the downloaded AK/SK file. Generally, the file name will be <strong id="modelarts_03_0004__b104721755202315">credentials.csv</strong>.)</span><p><div class="note" id="modelarts_03_0004__note322121052310"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="modelarts_03_0004__p16148151217238">If you use a RightCloud account, contact the system administrator to obtain the AK/SK.</p>
</div></div>
<ol type="a" id="modelarts_03_0004__ol527210192713"><li id="modelarts_03_0004__li206391249162717">Log in to the console, enter the <strong id="modelarts_03_0004__b202070308417">My Credentials</strong> page, and choose <strong id="modelarts_03_0004__b7208123016417">Access Keys</strong> &gt; <strong id="modelarts_03_0004__b102084303419">Create Access Key</strong>.</li><li id="modelarts_03_0004__li727212120279">In the <span class="wintitle" id="modelarts_03_0004__wintitle153673654211"><b>Create Access Key</b></span> dialog box that is displayed, use the login password for verification.</li><li id="modelarts_03_0004__li6272913275">Click <strong id="modelarts_03_0004__b16018498427">OK</strong>, open the <strong id="modelarts_03_0004__b86094914425">credentials.csv</strong> file, and save the key file as prompted. The access key file is saved in the default downloads folder of the browser. Then, the access key (<strong id="modelarts_03_0004__b2219107154314">Access Key Id</strong> and <strong id="modelarts_03_0004__b221927164310">Secret Access Key</strong>) is obtained.</li></ol>
</p></li><li id="modelarts_03_0004__li157891312153613"><span>Download and decompress the demo project.</span></li><li id="modelarts_03_0004__li94791126103617"><a name="modelarts_03_0004__li94791126103617"></a><a name="li94791126103617"></a><span>Import the demo project to Eclipse.</span><p><div class="fignone" id="modelarts_03_0004__fig126185494010"><span class="figcap"><b>Figure 2 </b>Selecting Existing Projects into Workspace</span><br><span><img id="modelarts_03_0004__image19645414403" src="en-us_image_0000001816121182.png"></span></div>
<div class="fignone" id="modelarts_03_0004__fig192514235413"><span class="figcap"><b>Figure 3 </b>Selecting the demo project</span><br><span><img id="modelarts_03_0004__image7251152384120" src="en-us_image_0000001815961414.png"></span></div>
<div class="fignone" id="modelarts_03_0004__fig1620863824118"><span class="figcap"><b>Figure 4 </b>Structure of the demo project</span><br><span><img id="modelarts_03_0004__image1520863816417" src="en-us_image_0000001862721209.png"></span></div>
</p></li><li id="modelarts_03_0004__en-us_topic_0094388456_li55824361184055"><span>Sign the request.</span><p><p id="modelarts_03_0004__en-us_topic_0094388456_p32657207184055">The request signing method is integrated in the JAR files imported in <a href="#modelarts_03_0004__li94791126103617">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="modelarts_03_0004__p48653222398">The demo code is classified into the following classes to demonstrate signing and sending the HTTP request:</p>
<ul id="modelarts_03_0004__ul1486512220397"><li id="modelarts_03_0004__li38651222193913"><strong id="modelarts_03_0004__b474718717244">AccessService</strong>: abstract class that merges the GET, POST, PUT, and DELETE methods into the <strong id="modelarts_03_0004__b4906511279">access</strong> method</li><li id="modelarts_03_0004__li586517224394"><strong id="modelarts_03_0004__b1794013111117">Demo</strong>: execution entry used to simulate the sending of GET, POST, PUT, and DELETE requests</li><li id="modelarts_03_0004__li1786522203918"><strong id="modelarts_03_0004__b27821111172414">AccessServiceImpl</strong>: implementation of the <strong id="modelarts_03_0004__b1851311365283">access</strong> method, which contains the code required for communication with API Gateway</li></ul>
<p id="modelarts_03_0004__en-us_topic_0094388456_p27988144184055">The following describes how to call a POST method to sign the request.</p>
<ol type="a" id="modelarts_03_0004__en-us_topic_0094388456_ol50566709184055"><li id="modelarts_03_0004__en-us_topic_0094388456_li52447197184055">Add request header fields.<p id="modelarts_03_0004__p11504143117410"><a name="modelarts_03_0004__en-us_topic_0094388456_li52447197184055"></a><a name="en-us_topic_0094388456_li52447197184055"></a>Comment out the following second line of code in the <strong id="modelarts_03_0004__b165552056182616">AccessServiceImpl.java</strong> file, and specify the project ID.</p>
<pre class="codeblock" id="modelarts_03_0004__codeblock9890155315414">//<strong id="modelarts_03_0004__b48999530411">TODO</strong>: Add special headers.
//request.addHeader("X-Project-Id", "<strong id="modelarts_03_0004__b9899165315411">xxxxx</strong>");</pre>
</li><li id="modelarts_03_0004__en-us_topic_0094388456_li49063522184055">Edit the <strong id="modelarts_03_0004__b1733816292307">main()</strong> method in the Demo.java file, and replace the bold text with actual values.<p id="modelarts_03_0004__p17237183119420">As shown in the following code, if you use other methods such as POST, PUT, and DELETE, see the corresponding comment. Replace the values of <strong id="modelarts_03_0004__b7624114719252">region</strong>, <strong id="modelarts_03_0004__b176259478254">serviceName</strong>, <strong id="modelarts_03_0004__b26263479259">ak</strong>, <strong id="modelarts_03_0004__b12627194711255">sk</strong>, and <strong id="modelarts_03_0004__b17627647152513">url</strong>. The URL for obtaining the VPC is used in the sample project. Replace it with the actual URL. For details about how to obtain the endpoint, see <a href="modelarts_03_0141.html">Regions and Endpoints</a>.</p>
<pre class="codeblock" id="modelarts_03_0004__codeblock13684120204813">//TODO: Replace the value of <strong id="modelarts_03_0004__b141216594494">region</strong> with the actual region where the service to be accessed is located.
private static final String region = "";
//TODO: Replace vpc with the name of the service you want to access. For example, ecs, vpc, iam, and elb.
private static final String serviceName = "";
public static void main(String[] args) throws UnsupportedEncodingException
{
//TODO: Replace the values of <strong id="modelarts_03_0004__b12131967518">ak</strong> and <strong id="modelarts_03_0004__b204417975116">sk</strong> with the AK/SK obtained on the <strong id="modelarts_03_0004__b1974751916510">My Credentials</strong> page.
String ak = "<strong id="modelarts_03_0004__b18861248596">ZIRRKMTWP******1WKNKB</strong>";
String sk = "<strong id="modelarts_03_0004__b1682213519118">Us0mdMNHk******YrRCnW0ecfzl</strong>";
//TODO: To specify a project ID (multi-project scenarios), add the X-Project-Id header.
//TODO: To access a global service, such as IAM, DNS, CDN, and TMS, add the X-Domain-Id header to specify an account ID.
//TODO: To add a header, find "Add special headers" in the AccessServiceImple.java file.
//TODO: Test the API.
String url = "<strong id="modelarts_03_0004__b1665025412113">https://{Endpoint}/v1/{project_id}/vpcs</strong>";
get(ak, sk, url);
//TODO: When creating a VPC, replace {project_id} in postUrl with the actual value.
//String postUrl = "https://serviceEndpoint/v1/{project_id}/cloudservers";
//String postbody ="{\"vpc\": {\"name\": \"vpc\",\"cidr\": \"192.168.0.0/16\"}}";
//post(ak, sk, postUrl, postbody);
//TODO: When querying a VPC, replace {project_id} in url with the actual value.
//String url = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}";
//get(ak, sk, url);
//TODO: When updating a VPC, replace {project_id} and {vpc_id} in putUrl with the actual values.
//String putUrl = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}";
//String putbody ="{\"vpc\":{\"name\": \"vpc1\",\"cidr\": \"192.168.0.0/16\"}}";
//put(ak, sk, putUrl, putbody);
//TODO: When deleting a VPC, replace {project_id} and {vpc_id} in deleteUrl with the actual values.
//String deleteUrl = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}";
//delete(ak, sk, deleteUrl);
}</pre>
</li><li id="modelarts_03_0004__en-us_topic_0094388456_li65392176184055">Compile the code and call the API.<p id="modelarts_03_0004__p723410494918"><a name="modelarts_03_0004__en-us_topic_0094388456_li65392176184055"></a><a name="en-us_topic_0094388456_li65392176184055"></a>In the <strong id="modelarts_03_0004__b1145145975210">Package Explorer</strong> area on the left, right-click <strong id="modelarts_03_0004__b184984152537">Demo.java</strong> and choose <strong id="modelarts_03_0004__b139222015530">Run AS</strong> &gt; <strong id="modelarts_03_0004__b92658274538">Java Application</strong> from the shortcut menu to run the demo code.</p>
<p id="modelarts_03_0004__p4234124134916">You can view the API call 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="modelarts_03_0144.html">Calling APIs</a></div>
</div>
</div>