- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试连接到 Oxford Dictionaries API .当我将 python 与此代码一起使用时,我没有遇到任何问题:
import requests
import json
app_id = 'my id'
app_key = 'my key'
language = 'en'
word_id = 'Ace'
url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word_id.lower()
r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})
print("code {}\n".format(r.status_code))
但是在 c# 中,我收到错误 403 Authentication failed using the following code:
HttpWebRequest req = null;
string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/"
string uri = PrimeUrl+word ;
req = (HttpWebRequest)HttpWebRequest.Create(uri);
string credentials = String.Format("{0}:{1}", uid, pwd);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
req.Headers.Add("Authorization", authorization);
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
HttpWebResponse HWR_Response = (HttpWebResponse)req.GetResponse();
我仔细检查了 id 和 key 是否正确,也尝试了所有建议 here .
发布这个问题是我最后的选择。谢谢。
最佳答案
这是将您的开发人员凭据正确添加到请求的方法 - 它们是 header :
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace OxfDictionary
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest req = null;
string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/";
string uri = PrimeUrl + "robot";
req = (HttpWebRequest)HttpWebRequest.Create(uri);
//These are not network credentials, just custom headers
req.Headers.Add("app_id", "5a......3");
req.Headers.Add("app_key", "d12............1a0");
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
using (HttpWebResponse HWR_Response = (HttpWebResponse)req.GetResponse())
using (Stream respStream = HWR_Response.GetResponseStream())
using (StreamReader sr = new StreamReader(respStream, Encoding.UTF8))
{
string theJson = sr.ReadToEnd();
Debug.WriteLine(theJson);
Console.WriteLine(theJson);
}
Console.ReadKey();
}
}
}
结果是:
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "robot",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"etymologies": [
"from Czech, from robota forced labour. The term was coined in K. Čapek's play R.U.R. Rossum's Universal Robots (1920)"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"senses": [
{
"definitions": [
"a machine capable of carrying out a complex series of actions automatically, especially one programmable by a computer:"
],
"domains": [
"Electronics"
],
"examples": [
{
"text": "a robot arm"
},
{
"text": "half of all American robots are making cars or trucks"
}
],
"id": "m_en_gb0714510.001",
"subsenses": [
{
"definitions": [
"(especially in science fiction) a machine resembling a human being and able to replicate certain human movements and functions automatically:"
],
"examples": [
{
"text": "the robot closed the door behind us"
}
],
"id": "m_en_gb0714510.002"
},
{
"definitions": [
"a person who behaves in a mechanical or unemotional manner:"
],
"examples": [
{
"text": "public servants are not expected to be mindless robots"
}
],
"id": "m_en_gb0714510.003"
}
]
},
{
"crossReferenceMarkers": [
"another term for crawler (in the computing sense)"
],
"crossReferences": [
{
"id": "crawler",
"text": "crawler",
"type": "see also"
}
],
"domains": [
"Computing"
],
"id": "m_en_gb0714510.004"
},
{
"definitions": [
"a set of automatic traffic lights:"
],
"domains": [
"Motoring"
],
"examples": [
{
"text": "waiting at a robot I caught the eye of a young woman"
}
],
"id": "m_en_gb0714510.005",
"regions": [
"South African"
]
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"pronunciations": [
{
"audioFile": "http://audio.oxforddictionaries.com/en/mp3/robot_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "ˈrəʊbɒt"
}
],
"text": "robot"
}
],
"type": "headword",
"word": "robot"
}
]
}
关于c# - 将 HttpWebRequest 凭证传递给 Oxford API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42040044/
我正在尝试连接到 Oxford Dictionaries API .当我将 python 与此代码一起使用时,我没有遇到任何问题: import requests import json app_id
我是高中 iOS Swift 应用程序开发类的学生。我正在尝试访问牛津英语词典 API 中的定义键,它嵌套在多个数组和词典中。我已经访问了“lexicalEntries”键内的数据,但我无法在 API
我偶然发现了 Project Oxford,并对它产生了浓厚的兴趣,并使用了它的 API,尤其是情感 API。微软提供示例代码 ########### Python 2.7 #############
您好,我尝试在不使用预训练权重的情况下实现 AlexNet。我尝试在 Oxford-102 数据集上训练网络,但在整个过程中我一直获得 0.9% 的准确率,并且更改参数没有帮助,下面的代码有人可以帮助
我已经查过了,我发现我可以通过 .dll 文件加载 API。但我一直认为我让一切变得过于复杂......有没有更简单的方法来做到这一点?提前致谢。 最佳答案 我建议您使用Cognitive Servi
我希望能够在我的网站上实现 Project Oxford 的 Emotion API。我目前编写了以下 HTML/JavaScript 代码,该代码检查来自 URL 的图像并在运行 Emotion A
我一直无法找到链接未被破坏的相关文档。 我想在我的 WPF 应用程序中实现 Project Oxford 的 Speech API 的所有 API 功能。这些功能包括添加、编辑和删除意图、实体、预建实
Microsoft Project Oxford有一个很好的语音识别 API 和 IOS 上 Objective-C 的说明。我按照入门说明轻松构建它。但是,我很难将它转换为 Swift 语言。 我首
任何人都可以获得 Azure 项目牛津语音 API 的有效 jQuery REST 代码示例。 我已启动 Azure 应用程序服务和 key 。只需要一个简单的原型(prototype)页面,在加载时
我正在尝试通过 API 调用从 Microoft Project Oxford 获取 JSON 数据。我关注了the API 引用,但是当我进行调用时出现 404 错误。
我是一名优秀的程序员,十分优秀!