Inside the //
comment section of the presentation code, I would like to get the code on the program instead of launching the browser by launching Process.Start
, but I don't know how to implement it.
I'd like to get the code = part of the URL that appears when I start my browser.
https://mstdn.jp/oauth/authorize/native?code=XXXXXXXXXXXXXXXXXXXX
Get the code by Process.Start()
client.PostAsync
attempted to retrieve but did not get anything.
*json and query are different things, but I just checked them, so I wrote them down.
I tried to use it again by pasting the code I got once to the variable, but I got an error.
Reference (https://mastodon.example/oauth/authorize portion):https://docs.joinmastodon.org/methods/apps/oauth/
The site I looked up
A https://learn.microsoft.com/ja-jp/dotnet/api/system.net.httpwebrequest?view=net-7.0
/*##################################################################################################################
* the constructor
###################################################################################################################*/
public Client(string client_id,string url)
{
instance = url;
client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("test/1.0"));
Process.Start("explorer.exe", $"\"https://{instance}/oauth/authorize?client_id={client_id}&scope=read&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code\"");
Console.Write("Input code:");
auth_code = Console.ReadLine();
}
Before that, understand the OAuth 2.0 authorization flow that Mastodon adopted.
cites an illustration of the authorization code flow from .
Browsers launched in Process.Start()
correspond to the flow 2-5 in the figure, and Console.ReadLine()
receives the authorization code displayed in the browser corresponding to Figure 6.
By the way, /oauth/token
is accessed from 7-8 in the figure.
I want to get that code on the program
If you use a web browser already logged in to the service (in this case, Mustodon), 3-5 will be omitted, so you may want to get the code directly or you may be able to do so, but if you use another browser, you will eventually be asked to log in.
You can receive it by converting it into a web service and specifying the address of the web service in redirect_uri
for the program to retrieve it.This step is required as long as you are running on the command line without web service.(Depending on the program's conditions, other steps may work, but I don't know the background, so I can only guide you through the general steps.)
© 2024 OneMinuteCode. All rights reserved.