'How do I enable end users other than myself to post Twitter? C#

I'm developing in C#. I want to post to Twitter. I am using CoreTweet. I am using my own ConsumerKey, ConsumerSecret, AccessToken, and AccessSecret. In this state, I can post to Twitter with my ID with the following code. In order to make this application available to the public, I need to make it possible for end users other than myself to post using their IDs.

Q1) How do I enable end users other than myself to post? Q2) Do I need ID/PW/authentication, etc. for end users other than myself? Q3) If so, how do I set this up? Q4)Can I embed ConsumerKey in my program? Q5)Can I embed a ConsumerSecret in my program? Q6) Can I embed an AccessToken in my program? Q7) Can I embed AccessSecret in my program?

using CoreTweet;
using System;
using System.Net;
using System.Net.Http;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;

            TweetCommand = new Command(OnTweetClicked);
        public Command TweetCommand { get; }

        private void OnTweetClicked(object obj)
        {
            string ConsumerKey = "";
            string ConsumerSecret = "";
            string AccessToken = "";
            string AccessSecret = "";

            var session = OAuth.Authorize(ConsumerKey, ConsumerSecret);
            var client = new HttpClient();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            Tokens tokens = Tokens.Create(ConsumerKey, ConsumerSecret, AccessToken, AccessSecret);
            Status statusResult = tokens.Statuses.Update(new { status = "test tweet"});
        }
    }
}

test project url is https://github.com/misakikaoru/SG2022

I then looked into it and was able to allow the end user to Tweet to the end user's ID, not the developer's, by entering a Pin code. The code is as follows

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ScrollView Grid.Row="0">
            <StackLayout Orientation="Vertical" Padding="10" Spacing="10">
                <WebView Source="{Binding Url}" HeightRequest="500"></WebView>
                <Entry Text="{Binding PinCode}" Keyboard="Numeric"/>
                <Button VerticalOptions="Center" Text="TweetWithPin" Command="{Binding TweetPinCommand}"/>
            </StackLayout>
        </ScrollView>
    </Grid>
        public AboutViewModel()
        {
            Title = "About";
            OpenWebCommand = new Command(async () => await Browser.OpenAsync("https://aka.ms/xamarin-quickstart"));
            TweetPinCommand = new Command(OnTweetPinClicked);
            Url = session.AuthorizeUri.AbsoluteUri;
        }

        public ICommand OpenWebCommand { get; }
        public Command TweetCommand { get; }
        public Command TweetPinCommand { get; }
        private WebViewSource url;
        public WebViewSource Url { get => url; set => SetProperty(ref url, value); }
        private string pinCode;
        public string PinCode { get => pinCode; set => SetProperty(ref pinCode, value); }

        //API key
        static string ConsumerKey = "";
        //API secret key
        static string ConsumerSecret = "";
        OAuth.OAuthSession session = OAuth.Authorize(ConsumerKey, ConsumerSecret);

        private void OnTweetPinClicked(object obj)
        {
            if (string.IsNullOrEmpty(PinCode)) return;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            try
            {
                var browserTokens = OAuth.GetTokens(session, PinCode);
                Status statusResult = browserTokens.Statuses.Update(new { status = "test tweet by user" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ffff") });
            }
            catch (Exception exception)
            {
            }
        }

Here are the answers so far. A1) Open the website and get the Pin, then use the Pin to OAuth.GetTokens. A2) Authentication and Pin are required. A3)Open the website, get the PIN and set it. A4) ConsumerKey needs to be embedded in the program. A5) ConsumerSecret needs to be embedded in the program. A6) AccessToken is not required. A7) AccessSecret is not required.

Now, I have an additional question. Q8)What is the length of a session? Q9)How long is the validity period of a Pin obtained in a session? Q10) If I save a Pin in the application, can I use the saved Pin when I close the application and start it again? Q11) Is it possible to get a Pin automatically?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source