简体   繁体   中英

REST API: Combine login information and API action using C#?

I am trying to use name.com API in C#. The API has different actions including /Login, /account/get, /create/domain and more. You can find the documentation here

The /Login method is POST and the body is a JSON, and done it with HttpWebRequest and it works successfully. It returns something like:

    {
        "result": {
                    "code":100,
                    "message":"Command Successful"
                  },
        "session_token":"a59a09be6562e977a166fdd2b345a235c8b6c724"
    }

But after login, I need to use the other services (run the other API actions).

Here I am sending another HttpWebRequest as:

    string base = "https://api.dev.name.com/api/"
    HttpWebRequest request;
    request = WebRequest.Create(base + "account/get") as HttpWebRequest;  
    ....
    ....

Instead of returning the account information, it returns an error which says:

    {
      "result": 
      {
        "code":251,
        "message":"Authentication Error - No Api Session Or Username Token Supplied"
      }
    }

I think that I need a way to send the request along with the login information. The err message here is "Authentication Error - No Api Session Or Username Token Supplied"

NOTE: There is PHP sample code and classes for this API. Here is the link of the API Documentation

See page 18 of the documentation . You need to pass the returned session token as a request header:

Api-Session-Token: <session_token>

So, once you get the response and get the token, you would just add the following line:

httpWebRequest.Headers.Add("Api-Session-Token", <session_token>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM