简体   繁体   English

在 API HTTP 调用中使用永久令牌

[英]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.

相关问题 在REST API中使用HttpClient将授权永久令牌标头添加到POST JSON - Add authorization permanent token header to POST JSON using HttpClient in REST API 验证用户并调用 REST API sharepoint 本地使用令牌 - Authenticate user & call REST API sharepoint on-premise using token 如何使用System.Net.Http将access_token和id_token发送到api - How to send an access_token and id_token to an api using System.Net.Http 如何使用较小的超时从http客户端调用Web API - How to call web API from an http client using a small timeout 获取永久的Facebook访问令牌 - get permanent facebook access token PayPal API Http调用问题 - Problems with PayPal API Http call 跨HTTP方法的永久重定向? - Permanent redirect across HTTP methods? 无法使用http客户端获取asp.net Web API令牌 - Cant get the asp.net Web API token using http client 尝试使用“ http:// localhost:61961 / Token”登录到Web API。 收到400错误的请求错误 - Trying to Login to the Web API using the “http://localhost:61961/Token”. Receiving 400 Bad Request Error 使用JWT令牌使访问令牌过期时刷新令牌调用 - Refresh Token call when Access token expired using JWT token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM