通过HttpWebRequest从C#连接到Tagpacker.com
有人知道我如何发布到该网站(在.Net中使用以下内容)吗?因为不知何故,我不了解对其服务器的授权 我当前使用的代码如下所示:通过HttpWebRequest从C#连接到Tagpacker.com,c#,http,authorization,httpwebrequest,C#,Http,Authorization,Httpwebrequest,有人知道我如何发布到该网站(在.Net中使用以下内容)吗?因为不知何故,我不了解对其服务器的授权 我当前使用的代码如下所示: private readonly HttpWebRequest _httpWebRequest = (HttpWebRequest) WebRequest.Create("https://tagpacker.com/api/users/{YourUserID}/tags"); public void Load() { _httpWebR
private readonly HttpWebRequest _httpWebRequest = (HttpWebRequest)
WebRequest.Create("https://tagpacker.com/api/users/{YourUserID}/tags");
public void Load()
{
_httpWebRequest.ContentType = "application/json";
_httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(_httpWebRequest.GetRequestStream()))
{
var json = "{\"user\":\"test\"," +
"\"password\":\"bla\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)_httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
但我得到了以下信息:
{
"status": 401,
"message": "User not authorized",
"code": "USER_NOT_AUTHORIZED",
"url": "/unauthorizedHandler"
}
你从他们那里得到的唯一东西就是“API访问”代码如下:1b3f314dd132b0015b5415b1:bd0ffe4b-a01e-4bf5-b1ed-12b24145d12d其中第一部分是您的用户id,第二部分是您的API代码我想?有人知道如何使用它吗?tagpacker团队的官方答案是您需要在t中设置API键其标题如下所示(Java示例):
URL=新URL(“https://tagpacker.com/api/users//links?limit=20");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
setRequestProperty(“api_键”,即“”);
我将尝试在C#中执行此操作,然后再次编辑答案
URL url = new URL("https://tagpacker.com/api/users/<your_user_id>/links?limit=20");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("api_key", "<your_api_key>");