无法制作第三方API请求(Unable to make third-party API Request)
我正在开发一个Ionic 2应用程序,它使用牛津词典中的第三方API。 他们的API需要
app_id
和app_key
作为身份验证步骤才能工作。以下是使
GET
请求调用API的服务@Injectable() export class OxfordVocabularyService { private app_id: any; private app_key: any; private OxfordBaseRequestURL: any; constructor(private http: Http) { this.app_id = configuration.oxfordAPI.app_id; this.app_key = configuration.oxfordAPI.app_key; this.OxfordBaseRequestURL = configuration.oxfordAPI.requestURL; } public getWordIPA(word: String): Promise<any> { let headers = new Headers({ 'Accept': 'application/json' }); headers.append('app_id', this.app_id); headers.append('app_key', this.app_key); let options = new RequestOptions({ headers: headers }); return this.http.get(this.OxfordBaseRequestURL + word + '/pronunciations', options).toPromise().then(response => { console.log(response.json()); }); }
我已经在标头请求上附加了
app_id
和app_key
,为什么我会收到错误。403 - 缺少认证参数
这是与CORS有关的吗? 你能告诉我正确调用API的方法吗?
更新
非常感谢@suraj和@ n00dl3,我通过在Ionic配置中添加代理解决了这个问题。
ionic.config.json
"proxies": [ { "path": "/oxfordapi", "proxyUrl": "https://od-api.oxforddictionaries.com/api/v1/entries/en" } ]
并将
GET
请求路径更改为this.http.get('oxfordapi/' + word + '/pronunciations', options)
I'm working on an Ionic 2 Application, which use third-party API from Oxford Dictionary. Their API require
app_id
andapp_key
as authentication step to work.Here is the service which make the
GET
request call to the API@Injectable() export class OxfordVocabularyService { private app_id: any; private app_key: any; private OxfordBaseRequestURL: any; constructor(private http: Http) { this.app_id = configuration.oxfordAPI.app_id; this.app_key = configuration.oxfordAPI.app_key; this.OxfordBaseRequestURL = configuration.oxfordAPI.requestURL; } public getWordIPA(word: String): Promise<any> { let headers = new Headers({ 'Accept': 'application/json' }); headers.append('app_id', this.app_id); headers.append('app_key', this.app_key); let options = new RequestOptions({ headers: headers }); return this.http.get(this.OxfordBaseRequestURL + word + '/pronunciations', options).toPromise().then(response => { console.log(response.json()); }); }
When the app run the service call, it throws err
The API request works perfectly fine on Postman
I have attached the
app_id
andapp_key
on headers request, so why do i get the error.403 - Authentication Parameters missing
Is this something related to CORS ? Can you show me the way to make the API call properly ?
Update
Huge thanks to @suraj and @n00dl3, i have solved the problem by adding a proxy to the Ionic Configuration.
ionic.config.json
"proxies": [ { "path": "/oxfordapi", "proxyUrl": "https://od-api.oxforddictionaries.com/api/v1/entries/en" } ]
And change the
GET
request path tothis.http.get('oxfordapi/' + word + '/pronunciations', options)
原文:https://stackoverflow.com/questions/43584752
最满意答案
简短的回答是“是的”。
简化的长答案是“是的,尽管发电机有一些开销(在某种意义上是固定的,它不会随着要生成的项目数量而变化),这使得当要生成的项目数量很少时,节省的成本就会消失。” (但当然,在这种情况下,总成本足够小,无论如何都很重要)
对于长期答案过于复杂的警告:
CPython(参考Python解释器)对代码的优化很少; 它可以在非常有限的情况下进行小的窥视孔优化(例如,它可以在字节代码中将
1 + 2 + x
转换为3 + x
,但由于运算符重载和操作顺序,它无法将x + 1 + 2
转换为x + 3
,因为它不能假设x + 1
将返回一个int
,并且它无法知道对于x
证明的任何类型,加法是关联的。 因此,当您在CPython中使用生成器时,它将始终在运行时作为生成器执行。相比之下,在大多数现代浏览器中,JavaScript引擎使用JIT-ing将JavaScript代码编译为本机代码; 它可以进行推测/自适应优化 ,它可以编译为假定特定类型和值的代码,并且只有在假设失败时才会依赖于解释原始JS。 这意味着你无法确切地说出在热循环中执行生成器代码的情况下将会采取什么措施(对于长期节省而言,昂贵的分析和优化将被认为是值得的)。
如果JS引擎确定生成器通常会产生少量可以提前计算的输出(通常是完全消耗的),那么生成过程没有明显的副作用,并且它会更有效而不是如果内存密集,那么生成本机代码并不能实际创建或运行生成器,而是产生一个输出
Array
(或者用于数字之类的东西,可能是ES6 类型的数组以减少内存使用) 。 我不了解哪些JS引擎(如果有的话)可能尝试执行这样的优化,但考虑到过去七年左右JS引擎的最新技术发展变化(最常用的现代JS引擎是最慢的)可能至少比2008年9月1日发布V8之前最快的引擎快10倍,今天发生的优化可能很快就会明天改变。The short answer is "Yes."
The simplified long answer is "Yes, though generators have some overhead (fixed in the sense that it doesn't change with the number of items to be generated) that make the savings vanish when the number of items to be generated is small." (but of course, in that case, the total cost is small enough that it rarely matters anyway)
Excessively complex caveat to the long answer:
CPython (the reference Python interpreter) does very little optimization of code; it can make small peephole optimizations in very limited circumstances (e.g. it can convert
1 + 2 + x
to3 + x
in the byte code, but due to operator overloading and order of operations, it can't convertx + 1 + 2
tox + 3
, because it can't assumex + 1
will return anint
, and it can't know that addition is associative for whatever typex
turns out to be). So when you use a generator in CPython, it will always be executed as a generator at run time.By contrast, on most modern browsers, the JavaScript engine uses JIT-ing to compile the JavaScript code down to native code; it can make speculative/adaptive optimizations where it compiles to code that assumes specific types and values and falls back on interpreting the original JS only if the assumptions fail. Which means you can't actually say for sure what will be done in the case where your generator code is executed in a hot loop (where expensive analysis and optimization would be considered worthwhile for the long term savings).
If the JS engine determines that the generator is usually producing a small number of outputs that can be calculated ahead of time, that are usually fully consumed, that the generation process has no visible side-effects, and that it would be more efficient and not too memory intensive, it would be well within its rights to produce native code that doesn't actually create or run a generator, but instead produces an
Array
of outputs (or for stuff like numbers, perhaps an ES6 typed array to reduce memory usage). I have no insight into which JS engines, if any, might attempt to perform such an optimization, but given how rapidly state of the art has been changing for JS engines over the last seven years or so (the slowest commonly used modern JS engine is probably at least 10x faster than the fastest engine from Sept. 1, 2008, before V8 released), the optimizations that occur today could easily change tomorrow.
相关问答
更多-
请问,什么是yield to refunding?[2024-03-05]
A rate of return measuring the performance of a Refunded bond, from the time of purchase to its Refund date. ( http://www.fs.ml.com/help/glossary.asp?term=y) -
您应该使用yield*将控件传递给另一个可迭代对象https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/yield* 您还试图在非生成器函数内部生成,但您不能。 只需使用return function* test() { yield 1; yield* [2,3].map((x) => {return x}); yield 4; } var gen = test(); console.log ...
-
ES6产生多个发电机(ES6 yield multiple generators)[2023-05-28]
我正在寻找的其实是这样的: // correct, effects will get executed in parallel const [users, repos] = yield [ call(fetch, '/users'), call(fetch, '/repos') ] 这里call只是回复诺言 当我们产生一系列效果时,发生器会被阻止,直到所有的效果都被解决,或者一旦被拒绝(就像Promise.all的行为一样)。 What I was looking for was actuall ... -
您不能在异步函数中使用“return”指令。 而不是做“return(returnData)”,进行回调“回调(状态,代码,返回数据)”或简单地“回调(returnData)”。 console.log('Before http request') responseData = null; //Just declaration x.exec(params, function(status, data) { responseData = data;//You can check status to s ...
-
如何获得收益率指标?(How to get index of yield?)[2023-05-03]
我可能不完全明白你想要做什么,但也许下面的,不使用functools.partial() ,可能会有所帮助,因为它不需要索引值。 它所做的是将makePool()函数传递给一个迭代器,该迭代器将生成Parse()函数期望的配对值并将它们作为元组传递。 from multiprocessing.pool import ThreadPool def pairwise(iterable): "s -> (s0,s1), (s2,s3), (s4, s5), ..." a = iter(itera ... -
简短的回答是“是的”。 简化的长答案是“是的,尽管发电机有一些开销(在某种意义上是固定的,它不会随着要生成的项目数量而变化),这使得当要生成的项目数量很少时,节省的成本就会消失。” (但当然,在这种情况下,总成本足够小,无论如何都很重要) 对于长期答案过于复杂的警告: CPython(参考Python解释器)对代码的优化很少; 它可以在非常有限的情况下进行小的窥视孔优化(例如,它可以在字节代码中将1 + 2 + x转换为3 + x ,但由于运算符重载和操作顺序,它无法将x + 1 + 2转换为x + 3 , ...
-
这是同一件事。 将块传递给布局方法,该方法将在调用时呈现文档正文。 yield实际上是Ruby中的关键字。 可以定义名为“yield”的方法(例如,Enumerator执行此操作),但是每当您看到没有接收器的yield ,它始终是将控制传递给块的相同关键字。 It's the same thing. A block is passed to the layout method that will render the document body when called. yield is actually ...
-
必须像这样定义发电机功能 function * simpleGenerator() { # Note the `*` after `function` keyword yield "first"; yield "second"; yield "third"; }; var g = simpleGenerator(); console.log(g.next()); # { value: 'first', done: false } 引用ECMA 6的发电机功能和谐页面 , 函数 ...
-
在您的情况下,您可以将IEnumerable
直接传递给WriteFile: public static void WriteFile(IEnumerable lines) { // how to save the state, of observe an collection/stream??? using(var writer = new System.IO.StreamWriter("c:\\temp\\test.txt")) { ... -
我不确定这是怎么回事。 呃,是的,它应该不起作用 。 这只是因为Babel中的一个bug而起作用。 yield不返回函数引用,那么像(yield 2)那样的括号语句是什么 - 它们是没有实体的胖箭头匿名函数吗? 他们如何使用这样的部分应用程序? 不,它只是标准的功能应用,没有魔力。 yield 可以返回一个函数引用,当它发生时,这可能会起作用。 如果没有,它将在第三个.next()调用上抛出异常。 作为工作版本的示例: function* generatorFunction() { yield (yie ...