I would like to know how to send a request to WebAPI using the key obtained.

Asked 1 years ago, Updated 1 years ago, 281 views

Regarding the presentation code, use the client key, client secret, and access token registered from the web site development page. I would like to send a request to the API, but even if I go like the comment section of the presentation code, all of them get 403 errors.

マ This is the mastodon API

I want to know how to send a request to the API using the application I registered on the web page

I looked at the reference site and entered the key I registered as a trial, and tried various things, but all of them received errors.
https://mstdn.jp/api/v1/timelines/public I tried some that didn't require the request parameters, but I got the same error.

The authentication has been completed on the web page, and it has been quite a while, so
I don't think it's because of the time lapse error.

Reference: https://docs.joinmastodon.org/methods/accounts/
Tried request: https://docs.joinmastodon.org/methods/accounts/ #following


        static void Print()
        {
 // String url="https://mstdn.jp/api/v1/accounts?Authorization=-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&username=shigurechan&password=Shigurechan7240&agreement=true&locale=ja";
            // String url = "https://mstdn.jp/oauth/authorize?response_type=read&client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&redirect_uri=urn:ietf:wg:oauth:2.0:oob";
            // String url = "https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=urn:ietf:wg:oauth:2.0:oob";
            // String url = "https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=urn:ietf:wg:oauth:2.0:oob";
            // String url="https://mstdn.jp/api/v1/accounts/verify_credentials?Authorization=EedvO1rwNiq8-KJsRnRowCfak7KXc9ggmdQh84EI33k";
            String url="https://mstdn.jp/api/v1/timelines/public";

            WebRequest request = WebRequest.Create(url);
            Stream response_stream=request.GetResponse().GetResponseStream();
            StreamReader reader = new StreamReader(response_stream);
            varobj_from_json =JObject.Parse(read.ReadToEnd());

            Console.WriteLine(obj_from_json);

   
        }

        static void Main (string[]args)
        {
           Print();


            Console.ReadKey();
        }

c#

2022-11-14 18:23

1 Answers

Sample code for authentication.

C# recognizes the contents of the Main method even if it is written directly, and the Main method can be async, so you can take advantage of away.The WebRequest has been deprecated for a long time, so it uses a successor HttpClient.

using System.Diagnostics;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;

varclient_id=";// Application client key
varclient_secret=";// Application client secret

using var client=new HttpClient {BaseAddress=newUri("https://mstdn.jp")};
// Mustodon seems to require a User-Agent, or 403 when not configured.
client.DefaultRequestHeaders.UserAgent.ParseAdd("test/1.0");

{
    // Start the browser and ask the user to approve the application, and receive the code obtained.
    Process.Start("explorer.exe", $"\"{client.BaseAddress}/oauth/authorize?client_id={client_id}&scope=read&redirect_uri=urn:ietf:wg:oob&response_type="\")
    Console.Write("Input code:");
    varcode=Console.ReadLine();
    if(string.IsNullOrEmpty(code))
        through new Exception();

    // An access token is acquired by using the obtained code.
    var parameter = new Dictionary <string, string > {
        { "client_id", client_id},
        { "client_secret", client_secret},
        { "redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
        { "grant_type", "authorization_code"},
        { "code", code},
        { "scope", "read"},
    };
    using var response=wait client.PostAsync("https://mstdn.jp/oauth/token", newFormUrlEncodedContent(parameter)));
    vartoken=awat response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<Token>();

    // A client stores an access token.Please note that if you use this client to access other sites, this token will be leaked.
    client.DefaultRequestHeaders.Authorization=new AuthenticationHeaderValue("Bearer", "token.access_token");
}

{
    // Obtain the account information of the approved user.
    varaccount=wait client.GetFromJsonAsync<Account>("/api/v1/accounts/verify_credentials");

    // By using the account ID, the following of the approved user is acquired.
    var followers = wait client.GetStringAsync ($"/api/v1/accounts/{account.id}/following");
    Console.WriteLine (followings);
}

// JSON DECIRALIZATION RECORD
public record Token (string access_token);
public record account (string id);


2022-11-15 05:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.