stable diffusion webui login interface (login) api interface call (using C#)

Nag

This time, I will tell readers how to request the sd webui api [login] interface through C#. If readers find the article useful, please give [like] it. If you have any questions, you can ask in the comment area.

combat

1. Configure api enable parameters

When starting webui, you need to add [–api], [–api-auth account: password] and [–gradio-auth account: password] to enable. Turn it on as shown in the figure below.
Note: It is invalid to only set the password of –api-auth here. The login interface is used for ui page login. The original code does not have the api-auth interface to return the token. (Please correct me if I am wrong)

insert image description here

2. Run locally

Visit http://127.0.0.1:7860/docs to see the swagger document and find the [login] interface
insert image description here

3. Code combat

1. In the console program, [RestSharp] nuget package needs to be installed to make API requests.
insert image description here

2. Add the following code to your project, just run it directly, the request is the interface running locally, the following [http://127.0.0.1:7860/sdapi/v1/login] needs to be replaced by the reader's own path.
3. Set [username] and [password]. When starting the sd project here, the following account and password are used. Readers need to replace them with their own local ones. 4. It can be found that the obtained
response.content returns {"success ":true} without token.
5. Look at the response body through debug, and you can find that there is a token in the cookies. At this time, the goal can be achieved by obtaining the token in the cookies, as shown in the figure below.

using RestSharp;
//post方式进行登录,登录成功后,获取cookies中的token
var client = new RestClient("http://127.0.0.1:7860/login/");
var request = new RestRequest();
request.AddHeader("Accept", "*/*");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("username", "你的账号");//账号
request.AddParameter("password", "你的密码");//密码
var response = client.Post(request);
Console.WriteLine($"请求结果:{
      
      response.Content}");
//获取token
var res = response?.Cookies?["access-token"]?.Value;
Console.WriteLine($"获取到的token:{
      
      res}");

insert image description here

Guess you like

Origin blog.csdn.net/qq_40600379/article/details/131147790
Recommended