如何获取Bing Ads Api中的授权代码以进行长期身份验证

时间:2015-08-17 07:12:42

标签: c# bing bing-api

如何获取长期身份验证的授权码。我正在使用Bing Ads API v 9.0。

这是我的代码。

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);
            }

我正在关注this文档。

1 个答案:

答案 0 :(得分:0)

以下代码为网络生成授权码。

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);

Response.Redirect(urlstring)重定向到该页面。使用

在重定向页面上获取授权码
string authCode = Request.QueryString["code"];