简体   繁体   English

AngularJS在get请求中包含API密钥

[英]AngularJS include API key in a get request

I am trying to get information from a fantasy data API using AngularJS. 我正在尝试使用AngularJS从幻想数据API获取信息。 I am using $resource to perform my get request in my controller, but I haven't been able to figure out how to correctly include the API key. 我使用$ resource在我的控制器中执行我的get请求,但是我无法弄清楚如何正确地包含API密钥。 Do I need to include it as a header? 我是否需要将其作为标题包含在内? Thanks. 谢谢。

nflApp.controller('mainController', ['$scope','$resource','$routeParams', function($scope, $resource, $routeParams) {

$scope.fantasyAPI = $resource("https://api.fantasydata.net/nfl/v2/JSON/DailyFantasyPlayers/2015-DEC-28", { callback: "JSON_CALLBACK" }, { get: { method: "JSONP"}});

console.log($scope.fantasyAPI);

}]);

Below is the http request info from the site. 以下是该站点的http请求信息。 http请求信息

You should set a header with the API key, AngularJS will send them with every request in the following case: 您应该使用API​​密钥设置标头,AngularJS会在以下情况下将每个请求发送给它们:

 $http.defaults.headers.common["Ocp-Apim-Subscription-Key"] = key;

When adding '.common' you are telling angular to send this in every request so you do not need to add it to every resource that hits the API. 添加'.common'时,您会告诉angular在每个请求中发送此消息,因此您无需将其添加到每个访问API的资源中。

A easy way to do that is by creating your own interceptors from $httpProvider at "config" fase. 一个简单的方法是在$httpProvider的“config”fase创建自己的interceptors

To do that, just write something like: 要做到这一点,只需写下:

mymodule.config(['$httpProvider', function($httpProvider){

$httpProvider.interceptors.push(function ($q) {
            return {
                'request': function (config) {
                    config.headers['Ocp-Apim-Subscription-Key'] = SomeUserClass.AuthToken();

                    return config;
                },

                'response': function (response) {



                    return response;
                }
            };
        });

});

You need to modify request header in JSONP. 您需要在JSONP中修改请求标头。 Unfortunately it is not possible. 不幸的是,这是不可能的。 As the browser is responsible for header creation and you just can't manipulate that when using JSONP method. 由于浏览器负责创建标头,因此在使用JSONP方法时无法操纵它。

how to change the headers for angularjs $http.jsonp 如何更改angularjs $ http.jsonp的标题

Set Headers with jQuery.ajax and JSONP? 使用jQuery.ajax和JSONP设置标头?

From that link - https://johnnywey.wordpress.com/2012/05/20/jsonp-how-does-it-work/ 从该链接 - https://johnnywey.wordpress.com/2012/05/20/jsonp-how-does-it-work/

Why NOT To Use JSONP? 为什么不使用JSONP?
Deciding against using JSONP is directly related to how it works. 决定不使用JSONP与其工作方式直接相关。 First of all, the only HTTP method you can use is GET since that is the only method script tags support. 首先,您可以使用的唯一HTTP方法是GET,因为这是脚本标记支持的唯一方法。 This immediately eliminates the use of JSONP as an option to interact with nice RESTful APIs that use other HTTP verbs to do fun stuff like CRUD. 这立即消除了使用JSONP作为一个选项,与使用其他HTTP动词的好RESTful API交互来做有趣的事情,比如CRUD。 And while we're on the subject of GET, note that using anything other than URL parameters to communicate with the server API (eg sending some JSON over) is also not possible. 虽然我们讨论GET的主题,但请注意,使用除URL参数之外的任何内容来与服务器API进行通信 (例如,发送一些JSON)也是不可能的。 (You could encode JSON as a URL parameter, but shame on you for even thinking that.) (你可以将JSON编码为一个URL参数,但是即使你这么想也会感到羞耻。)

If they only work with header manipulation you will need to do that call from your server side. 如果它们只使用标头操作,则需要从服务器端进行该调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM