BIM360: C#如何发送HTTP GET和POST请求登陆BIM 360 Glue以及获取项目列表

登陆

首先是登陆,通过发送HTTP POST请求:

int timestamp = GetTimestamp();
string sig = GetSig(api_key, api_secret, timestamp);
string url = "https://b4.autodesk.com/api/security/v1/login.json";
HttpWebRequest oRequest = (HttpWebRequest)HttpWebRequest.Create(url);
oRequest.Method = "POST";
var postData =
    "pretty=1&" +
    "login_name=" + username +
    "&password=" + password +
    "&company_id=" + company_id +
    "&api_key=" + api_key +
    "&api_secret=" + api_secret +
    "&timestamp=" + timestamp +
    "&sig=" + sig;

textBoxRequest.Text = postData;

var data = Encoding.ASCII.GetBytes(postData);
oRequest.ContentType = "application/x-www-form-urlencoded";
oRequest.ContentLength = data.Length;

using (var stream = oRequest.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
string tBody = new StreamReader(oResponse.GetResponseStream()).ReadToEnd();

代码中HttpWebRequest的命名空间是System.Net

pretty=1参数会格式化返回的JSON,方便查看。

获取timestamp和sig的方法如下:

private int GetTimestamp()
{
    TimeSpan tSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
    int timestamp = (int)tSpan.TotalSeconds;
    return timestamp;
}

private string GetSig(string apikey, string apisecret, int timestamp)
{
    return ComputeMD5Hash(apikey + apisecret + timestamp);
}

登陆之后,返回的结果如下:

{
  "auth_token": "6df75b2959704f86b797fd4b5cbec6af",
  "user_id": "2274c83b-79fd-46ff-8772-38726b348c7f",
  "account_id": "1074a18f-91bf-4b8a-99ed-8d7625958619",
  "account_hostname": "adn"
}

获取项目列表

获取项目列表需要用GET方法,关键点在于如何构建请求的网址:
int timestamp = GetTimestamp();
string sig = GetSig(api_key, api_secret, timestamp);
string url = "https://b4.autodesk.com/api/project/v1/list.json";
var urlParameters =
    "pretty=1&" +
    "company_id=" + company_id +
    "&api_key=" + api_key +
    "&timestamp=" + timestamp +
    "&auth_token=" + auth_token +
    "&sig=" + sig;

url += "?" + urlParameters;

HttpWebRequest oRequest = (HttpWebRequest)HttpWebRequest.Create(url);
oRequest.Method = "GET";

HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
string tBody = new StreamReader(oResponse.GetResponseStream()).ReadToEnd();

返回结果:
{
  "project_list": [
    {
      "recent_model_info": [],
      "project_id": "1e7e5e8d-fea8-49cd-8e5b-76058f0ee3b6",
      "project_name": "AL+Sample+Project",
      "company_id": "adn",
      "created_date": "2015-03-19 03:26:51",
      "modify_date": "2015-05-06 14:45:40",
      "cmic_company_code": "",
      "cmic_project_code": "",
      "cw_project_code": "",
      "thumbnail_modified_date": "2015-03-19 04:41:45",
      "has_views": false,
      "has_markups": false,
      "has_clashes": false,
      "has_points": false,
      "total_member_count": 0,
      "total_project_admin_count": 0,
      "total_views_count": 0,
      "total_markups_count": 0,
      "last_activity_date": "2015-05-06 14:45:40",
      "permissions": [],
      "navisworks_version": "Nwd2014"
    }
  ],
  "page": 1,
  "page_size": 1,
  "total_result_size": 1,
  "more_pages": 0
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值