Xamarin.Auth - Get Authorization Code and Get Access Token

DiamondKeshaDiamondKesha Member ✭✭

How i can get access token?
Api shikimori -
Get Authorization Code - https://shikimori.one/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=code&scope=
Get Access Token - curl -X POST "https://shikimori.one/oauth/token" \ -H "User-Agent: APPLICATION_NAME" \ -F grant_type="authorization_code" \ -F client_id="CLIENT_ID" \ -F client_secret="CLIENT_SECRET" \ -F code="AUTORIZATION_CODE" \ -F redirect_uri="REDIRECT_URI"

and my code in app -
private void LogInShikimori(object sender, EventArgs e)
{

            var auth = new OAuth2Authenticator(
                clientId: "Mgjmzju3RuU2TyuGktAJIx5yHEV5K9QY2VFv-gJumww",
                scope: "user_rates",
                authorizeUrl: new Uri("https://shikimori.one/oauth/authorize"),
                redirectUrl: new Uri("urn:ietf:wg:oauth:2.0:oob"),
                clientSecret: "code",
                accessTokenUrl: new Uri("https://shikimori.one/oauth/token")
            );
            auth.AllowCancel = true;
            auth.Completed += OnAuthCompleted;

            var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
            presenter.Login(auth);
        }

        async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
        {
            if (e.IsAuthenticated)
            {
                String requestContent = $"grant_type=authorization_code&client_id=Mgjmzju3RuU2TyuGktAJIx5yHEV5K9QY2VFv-gJumww&client_secret=w9LT2Nj_EClfY1wVzZ5nPm8rGKCwM$code={e.Account.Properties["access_token"]}&redirect_uri=urn: ietf: wg: oauth: 2.0:oob";
                var Request = (HttpWebRequest)WebRequest.Create("https://shikimori.one/oauth/token");
                Request.UserAgent = "🔎 Search Anime";
                Request.Method = "POST";
                Request.ContentType = "application/x-www-form-urlencoded";

                byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestContent);
                Request.ContentLength = bytes.Length;
                using (System.IO.Stream outputStream = Request.GetRequestStream())
                {
                    outputStream.Write(bytes, 0, bytes.Length);
                }
                WebResponse webResponse = Request.GetResponse();

                DisplayAlert(null, webResponse.ToString(), null);
            }
        }

What am I doing wrong? Help me please

Sign In or Register to comment.