Aras V12.0 Restful API使用说明

十二月 11, 201913:05:40 发表评论 655 浏览

对于restful API的使用,官方实际也是花费了大心血的,毕竟这一块是以后的主流,能够有更强大的扩展性对于Aras来说无疑是更大的亮点,那么如何使用Restful API呢,实际上,官方的博客也花费了大量的篇幅来介绍API的使用,

以下为官方教程

Aras V12.0 Restful API使用说明

https://community.aras.com/b/english/posts/tech-tip-using-the-aras-restful-api

https://community.aras.com/b/english/posts/token-authentication-using-the-rest-api?CommentId=2b208166-fe9b-4e65-9c95-5ca60eaa5555

从这两篇文章中,你能够很好的找到如何使用Restful API的手段

那么下面我就将从C#和JAVA两种语言的角度去处理API

首先是针对框架原生语言C#的代码示例:

获取Token

innovatorUrl + "/Server/OAuthServerDiscovery.aspx

innovatorUrl 代表的是你系统的服务器的地址

var oauthServerUrl = GetOAuthServerUrl(innovatorServerDiscoveryUrl);

namespace ODataExample
{
    class Program
    {
		const string innovatorUrl = "http://localhost/OBOTService"; // base Innovator url
		const string innovatorUsername = "admin"; // Innovator user name
		const string innovatorPassword = "607920B64FE136F9AB2389E371852AF2"; // MD5 hash of Innovator user password (mush be SHA hash in case of FIPS)
        const string innovatorDatabase = "OBOTService"; // database name, could be obtained from innovatorServerUrl+"dblist.aspx"
        const string oauthServerClientId = "IOMApp"; // must be registered in authorization server's oauth.config file

        const string innovatorServerDiscoveryUrl = innovatorUrl + "/Server/OAuthServerDiscovery.aspx";



        static void Main(string[] args)
        {

            //
            // Get authorization server url
            // ============================
            var oauthServerUrl = GetOAuthServerUrl(innovatorServerDiscoveryUrl);

            if (oauthServerUrl == null)
                Environment.Exit(0);

            //
            // Get token endpoint
            // ==================
            var oauthServerConfigurationUrl = oauthServerUrl + ".well-known/openid-configuration";

            var tokenUrl = GetTokenEndpoint( oauthServerConfigurationUrl );

            if (tokenUrl == null)
                Environment.Exit(0);


            //
            // Get access token
            // ================
            var body = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("scope", "Innovator"),
                new KeyValuePair<string, string>("client_id", oauthServerClientId),
                new KeyValuePair<string, string>("username", innovatorUsername),
                new KeyValuePair<string, string>("password", innovatorPassword),
                new KeyValuePair<string, string>("database", innovatorDatabase),
            });

            var tokenData = GetJson(tokenUrl, null, body);

            if(tokenData == null)
                Environment.Exit(0);


            //
            // Request parts using OData
            // =========================
            string accessToken = tokenData.access_token;
            string odataUrl = innovatorUrl + "/server/odata/CAD";

            var parts = GetJson(odataUrl, accessToken);

            Console.WriteLine(parts);
        }
		
		
		static dynamic GetJson( string url, string accessToken = null, HttpContent body = null )
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json") );
                if(accessToken != null)
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                }

                HttpResponseMessage response;
                if (body == null)
                {
                    response = client.GetAsync(url).Result;
                }
                else
                {
                    response = client.PostAsync(url, body).Result;
                }

                if (response.IsSuccessStatusCode)
                {
                    return response.Content.ReadAsAsync<dynamic>().Result;
                }
                else
                {
                    Console.WriteLine("{0}: {1} ({2})", url, (int)response.StatusCode, response.ReasonPhrase);
                    return null;
                }

            }
        }

        static string GetOAuthServerUrl( string url )
		{
            var discovery = GetJson(url);

            return discovery?.locations[0]?.uri;
		}

        static string GetTokenEndpoint(string url)
        {
            var configuration = GetJson(url);

            return configuration?.token_endpoint;
        }

    }
}

Java代码

private void getToken() {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> params = new LinkedMultiValueMap();
        params.add("grant_type","password");
        params.add("client_id","IOMApp");
        params.add("scope","Innovator");
        params.add("username","root");
        params.add("password","607920b64fe136f9ab2389e371852af2");
        params.add("database","OBOTService");

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ArasRsp data = client.postForObject("http://localhost/OBOTService/oauthserver/connect/token", requestEntity, ArasRsp.class);

        log.info(data.getAccess_token());

        if(!data.getAccess_token().isEmpty())
        {
            getcad(data.getAccess_token());
        }

    }

    private void getcad(String bear)
    {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
        headers.setBearerAuth(bear);

        HttpEntity<?> entity = new HttpEntity<>(headers);

        HttpEntity<String> response = client.exchange(
               "http://localhost/OBOTService/server/odata/CAD",
                HttpMethod.GET,
                entity,
                String.class);

        log.info(response.toString());
    }
  • 微信
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信扫一扫
  • weinxin
admin

发表评论

您必须才能发表评论!