Want to know exactly how to configure WebHeaderCollection.Add()

Asked 1 years ago, Updated 1 years ago, 339 views

Regarding the presentation code, regarding the code for obtaining json from the API of the mastodon site below, why does the code inside the // comment cause an exception?I don't know why.

According to various investigations, there is no problem with the URL, and it seems that the problem is that the server is playing its own request, so I thought a similar setting was necessary this time as I set user-agent.
I would like to set the value to WebHeaderCollection.Add(), but all exceptions will occur.How do I set this value?

The remote server returned an error: (403) Not available

Rewrite URL Redirect URI
Review Reference Site
"http://www.google.co.jp/" allows code execution without 403
We have set req.Headers.Set to various values in reference to the request header site, but all of them have exceptions.

Create a new application to obtain OAuth2 credentials from the reference on the reference site.
Code for obtaining the json of the .

Reference: https://docs.joinmastodon.org/methods/apps/
GetResponce():https://learn.microsoft.com/ja-jp/dotnet/api/system.net.webrequest.getresponse?view=net-7.0
Error site: https://www.innovation.co.jp/urumo/http_error/
Request Header: https://developer.mozilla.org/ja/docs/Glossary/Request_header

private static async Task test_run()
        {            

            
//            var req = WebRequest.Create("http://www.google.co.jp/");
            var req = WebRequest.Create("https://mstdn.jp/api/v1/timelines/public");
            // var req = WebRequest.Create("https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=https%3A%2F%2Fmstdn.jp%2Fauth%2Fsign_in");
            // var req = WebRequest.Create("https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=urn:ietf:wg:oauth:2.0:oob");

            
            Console.WriteLine("aaaaa";
            ///////////////////////////////////////////////////////////////////////////////
            // req.Headers.Set ("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko");
            //req.Headers.Set ("Host", "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko");
            // req.Headers.Add ("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko");
            req.Headers.Add("Accept-Language:ja,en-us;q=0.7,en;q=0.3");


///////////////////////////////////////////////////////////////////////////////

            try
            {
                varres=req.GetResponse();
            } catch (System.Net.WebExceptione)
            {
                Console.WriteLine(e.Message);
            }

            //////////////////////////////////////////////////////////////////////////////
            Console.WriteLine("bbbbb";

            Console.ReadKey();
            /*
            // Convert Response (JSON) to Object
            ServiceResult info;
            using(res)
            {
                using(var resStream=res.GetResponseStream())
                {
                    varserializer = new DataContractJsonSerializer (typeof(ServiceResult)));
                    info=(ServiceResult) serializer.ReadObject(resStream);
                }
            }
            */

            Console.ReadKey();



        }


        private static async void test()
        {
            Task.WaitAll(test_run());

        }

        static void Main (string[]args)
        {
           // t();

            test();



        }


        DataContract
        public class ServiceResult
        {

            DataMember
            public string id {get;set;}
            DataMember
            public string name {get;set;}

        }

c#

2022-11-14 15:44

1 Answers

403Forbidden is an authentication error authorization error.The code in the question does not contain any authentication authorization actions, so it is natural that any specification results in an authorization error.On the other hand, it is natural that Google does not require authorization to fail.

Follow Logging in with an account to complete the authentication and authorization process before you do it.

First, if you have not already registered a client application, then see Creating our application on the previous page or go directly to POST/api/v1/apps for the full documentation of that method.We will need the client_id and

If you haven't read the content, you might think you can call /api/v1/apps, but authentication is also required to call this API."See Creating your application on the previous page" is the way to solve the chicken and egg condition.

Once you have registered, you should have obtained client_id (client key) and client_secret (client secret), so do the Authorize the user or later procedure.

var req = WebRequest.Create("https://mstdn.jp/api/v1/apps?client_name=test&redirect_uris=https%3A%2F%2Fmstdn.jp%2Fauth%2Fsign_in"); I still get 403 errors, but I think it's something else. What do you think?

"If you haven't read the content, you may think you can call /api/v1/apps, but authentication is also required to call this API."Read the answers carefully.


2022-11-14 23:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.