[英]Using Permanent Token in API HTTP Call
I'm writing a C# desktop app that will make some API calls to retrieve some data in JSON in which I'll parse.我正在编写一个 C# 桌面应用程序,它将进行一些 API 调用以检索 JSON 中的一些数据,我将在其中进行解析。 I'm using Deputy's API ( https://www.deputy.com/api-doc/API/Getting_Started ) and I'm having trouble getting it to work with a permanent token (I followed their steps and generated a token, so I have one).
我正在使用代理的 API ( https://www.deputy.com/api-doc/API/Getting_Started )并且我无法让它与永久令牌一起使用(我按照他们的步骤生成了一个令牌,所以我有一个)。 In the past I usually just do a
HttpWebRequest
and pass it the api url and it works fine.在过去,我通常只做一个
HttpWebRequest
并将其传递给 api url 并且它工作正常。 But in the past if there was an api key I would do something like &api_key=api_key_here
or if I had an authorization key it would be something like &Authorization=put_token_here
.但在过去,如果有一个 api 密钥,我会做类似
&api_key=api_key_here
的事情,或者如果我有一个授权密钥,它会是类似&Authorization=put_token_here
的事情。
So I'm just trying to get Roster working for example: /api/v1/supervise/roster/
(see https://www.deputy.com/api-doc/API/Roster_Management_Calls ).所以我只是想让名册工作,例如:
/api/v1/supervise/roster/
(参见https://www.deputy.com/api-doc/API/Roster_Management_Calls )。
So my code looks something like this:所以我的代码看起来像这样:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(
"https://mysite.na.deputy.com/api/v1/supervise/roster&Authorization=my_permanent_token_here"
);
httpWReq.Method = "GET";
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
this.Invoke((MethodInvoker)delegate
{
richTextBox1.AppendText(responseString + Environment.NewLine);
});
}
catch (Exception ex)
{
this.Invoke((MethodInvoker)delegate
{
richTextBox1.AppendText(ex.ToString());
});
}
}
But I'm getting the following:但我得到以下信息:
{"error":{"code":403,"message":"No authorization given"}}
{"error":{"code":403,"message":"没有授权"}}
I think it has to be the syntax on my permanent token, but I've tried api_key=
and token=
and authorization=
.我认为它必须是我的永久令牌上的语法,但我已经尝试过
api_key=
和token=
和authorization=
。 Can anyone help me out?谁能帮我吗?
You need to pass the token as a line in the request header in the following format:您需要在请求 header 中以以下格式将令牌作为一行传递:
Authorization: OAuth YOURTOKEN
Use the following code:使用以下代码:
string permanentAccessToken = "123456";
WebRequest request = WebRequest.Create("http://blabla");
request.Headers.Add("Authorization", "OAuth " + permanentAccessToken);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.