-3

I'm following this guide, until the final step when I have to get the access token from the URL. But it is appended behind a # character, and I have no idea how to get that part of the URL. Someone please tell me a code that works, or maybe a different approach to get that piece of information.

Negius
  • 1
  • 1
  • Is your URL a `string`? If so, you could use [`string.Split`](https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx). – Thomas Flinkow Jun 05 '18 at 09:55
  • 1
    What have you tried so far??? – BossRoss Jun 05 '18 at 09:57
  • nevermind, it was my fault when I skipped 1 line in that guide, which says I have to implement client-side code to get that fragment. All I need to do is put a little js script in the page. – Negius Jun 06 '18 at 03:37

2 Answers2

1

You need the fragment.

var fragment = new Uri("http://www.example.com/test#myfragment").Fragment;
Console.WriteLine(fragment);
Console.WriteLine(!string.IsNullOrEmpty(fragment) ? fragment.SubString(1) : string.Empty);

Try it online

Then you could extract the access token:

  1. Get the fragment
  2. Remove the #
  3. Split it based upon &
  4. Split each pair based on =
  5. Find the pair with the key "acces_token"
  6. Assign the pair to accessToken, or throw an exception if it's not found.

-

var fragment = new Uri("https://www.example.com/cb#access_token=bHLgV4q6--&token_type=bearer&xoauth_yahoo_guid=JTDI2OCE&state=XYZ").Fragment;
if (string.IsNullOrEmpty(fragment))
{
    throw new Exception("Access token not found.");
}
fragment = fragment.Substring(1);

var accessToken = fragment.Split(new[] { "&" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(p => p.Split('='))
                .FirstOrDefault(p => p.Length == 2 && p[0] == "access_token")?[1]
                    ?? throw new Exception("Access token not found");
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
0

Below program uses Regex, we have inputs and outputs, using Url Fragments in C#.

using System;

namespace Solutions
{
    using System.Text.RegularExpressions;

    class Program
    {
        static void Main()
        {
            const string RedirectUrl = "https://www.example.com/cb#access_token=bHLgV4q6--&token_type=bearer&xoauth_yahoo_guid=JTDI2OCE&state=XYZ";

            // Create Uri
            var uriAddress = new Uri(RedirectUrl);

            if (string.IsNullOrEmpty(uriAddress.Fragment))
            {
                Console.WriteLine("Null Fragment");
                return;
            }
            Console.WriteLine(uriAddress.Fragment);

            // To get complete appended Access code
            Console.WriteLine(uriAddress.Fragment.Substring(uriAddress.Fragment.IndexOf("#", StringComparison.CurrentCultureIgnoreCase)));

            // Apply Regex to get matches
            var regex = new Regex("([^?=&]+)(=([^&]*))?");
            var matches = regex.Matches(uriAddress.Fragment);
            foreach (Match match in matches)
            {
                var keyValueSplit = match.Value.Split(new[] { '=' }, 2, StringSplitOptions.None);
                Console.WriteLine(keyValueSplit[0] + " = " + keyValueSplit[1]);
            }

            Console.ReadLine();
        }
    }
}

Output:

#access_token=bHLgV4q6--&token_type=bearer&xoauth_yahoo_guid=JTDI2OCE&state=XYZ

#access_token=bHLgV4q6--&token_type=bearer&xoauth_yahoo_guid=JTDI2OCE&state=XYZ

#access_token = bHLgV4q6--

token_type = bearer

xoauth_yahoo_guid = JTDI2OCE

state = XYZ

vCillusion
  • 1,749
  • 20
  • 33