如何使用api密钥C#访问restful webservice [英] How can I access restful webservice using api key C#

查看:378
本文介绍了如何使用api密钥C#访问restful webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,当谈到restful api但是我知道如何使用登录名和密码凭据来安抚webservice但是我被要求使用api密钥和app秘密,我之前从未使用过访问url。我只是想问一下如何使用登录名和密码凭证来做这件事。



我尝试过:



 WebRequest request = WebRequest.Create(url); 
request.Method = WebRequestMethods.Http.Get;
NetworkCredential networkCredential = new NetworkCredential(登录,密码); //以domain\username格式登录
CredentialCache myCredentialCache = new CredentialCache {{new Uri(url),Basic,networkCredential}};
request.PreAuthenticate = true;
request.Credentials = myCredentialCache;
using(WebResponse response = request.GetResponse())
{
Console.WriteLine(((HttpWebResponse)response).StatusDescription);

使用(Stream dataStream = response.GetResponseStream())
{
使用(StreamReader reader = new StreamReader(dataStream))
{
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
}
}
}

解决方案

大多数API服务提供商都要求您发送API密钥 - 您已经拥有 - 通过HTTP标头,该标头由他们设置,您必须遵循惯例。例如,在Microsoft Azure上,您必须通过 Ocp-Apim-Subscription-Key 发送订阅密钥和令牌,然后Azure会检查此令牌并验证您的请求。



同样,您的要求是看看他们如何向您请求该令牌,因为您尚未共享您正在使用的服务我们无法向您提供任何文档链接;也许像{servicename} send token http这样的快速Google查询可能会产生一个好结果。 : - )

I am newbie when it comes to restful api but I know how to restful webservice using login and password credentials but I was required to use api key and app secret on which I never used before on accessing url. I just wanted to ask how to do this with login and password credentials.

What I have tried:

WebRequest request = WebRequest.Create(url);
        request.Method = WebRequestMethods.Http.Get;
        NetworkCredential networkCredential = new NetworkCredential(logon, password ); // logon in format "domain\username"
        CredentialCache myCredentialCache = new CredentialCache { { new Uri(url), "Basic", networkCredential } };
        request.PreAuthenticate = true;
        request.Credentials = myCredentialCache;
        using (WebResponse response = request.GetResponse())
        {
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);

            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(dataStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    Console.WriteLine(responseFromServer);
                }
            }
        }

解决方案

Most API service providers require you to send the API key — that you already have — via an HTTP header, that header is set by them and you must follow the convention. For instance, on Microsoft Azure you must send the subscription keys and tokens via "Ocp-Apim-Subscription-Key", and Azure then checks this token and validates your request.

Similarly, your requirement is to see how they request that token from you, as you have not shared the service that you are using we cannot provide any documentation links to you; perhaps a quick Google query like "{servicename} send token http" might generate a good result. :-)


这篇关于如何使用api密钥C#访问restful webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆