2

How to get authorization code for long term authentication. I am using Bing Ads API v 9.0.

Here is my Code.

string urlstring = "https://login.live.com/oauth20_authorize.srf?client_id=" + ClientId + "&scope=bingads.manage&response_type=code&redirect_uri=" + "https://login.live.com/oauth20_desktop.srf";
            var realUri = new Uri(urlstring,UriKind.Absolute);
            var addy = realUri.AbsoluteUri.Substring(0, realUri.AbsoluteUri.Length - realUri.Query.Length);
            var MyClient = WebRequest.Create(addy) as HttpWebRequest;
            MyClient.Method = WebRequestMethods.Http.Post;
            MyClient.Headers[HttpRequestHeader.AcceptLanguage]="en-us";
            MyClient.ContentType = "application/x-www-form-urlencoded";
            using (var writer = new StreamWriter(MyClient.GetRequestStream()))
            {
                writer.Write(realUri.Query.Substring(1));
            }

            var response = (HttpWebResponse)MyClient.GetResponse();

            for (int i = 0; i < response.Headers.Count; i++)
            {
                Console.WriteLine(response.Headers.GetKey(i) + " -- " + response.Headers.Get(i).ToString());
            }
            var responseSerializer = new DataContractJsonSerializer(typeof(AccessTokens));
            AccessTokens tokenResponse = null;
            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                    tokenResponse = (AccessTokens)responseSerializer.ReadObject(responseStream);
            }

I am following this documentation.

JAL
  • 41,701
  • 23
  • 172
  • 300
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65

1 Answers1

0

The below following code generates authorization code for web.

private const string ClientId = "xxxxxxxxxxxxxx";
 private const string RedirectionUri = "Your Redirection URL";
// Redirection URL should be defined during the app registration.
string urlstring = "https://login.live.com/oauth20_authorize.srf?client_id=" + ClientId + "&scope=bingads.manage&response_type=code&redirect_uri=" + RedirectionUri;
Response.Redirect(urlstring);

The Response.Redirect(urlstring) Redirected to the page. Get the authorization code on Redirection Page by using

string authCode = Request.QueryString["code"];
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65