简体   繁体   English

angularjs和google网址缩短器

[英]angularjs and googles url shortener

I have an issue at the moment with the google url shortener. 我目前与Google网址缩短器有问题。 I have set up this service: 我已经设置了此服务:

angular.module('widget.core').service('urlShortener', service);

function service($log, $q, $http) {

    var gapiKey = '<MyApiKey>';
    var gapiUrl = 'https://www.googleapis.com/urlshortener/v1/url';

    return {
        shorten: shorten
    };

    //////////////////////////////////////////////////

    function shorten(url) {
        console.log(url);
        var data = {
            method: 'POST',
            url: gapiUrl + '?key=' + gapiKey,
            headers: {
                'Content-Type': 'application/json',
            },
            data: {
                longUrl: url,
            }
        };

        return $http(data).then(function (response) {
            $log.debug(response);
            return response.data;
        }, function (response) {
            $log.debug(response);
            return response.data;
        });
    };
};

As far as I can tell, this should work. 据我所知,这应该工作。 I have put in the correct API key and when I run this method I get this error: 我输入了正确的API密钥,运行此方法时出现此错误:

{
    error: {
        code: 401,
        message: 'Invalid credentials'
    }
}

But, if I use postman and set it up exactly like this method: 但是,如果我使用邮递员并完全按照以下方法进行设置:

When I post this, it works with no issues. 当我发布此消息时,它没有任何问题。 I have checked my application on the google console and it is definitely set to unrestricted. 我已经在Google控制台上检查了我的应用程序,并且绝对将其设置为不受限制。

Has anyone come across this issue before? 有人遇到过这个问题吗? Does anyone know how to solve it? 有人知道如何解决吗?

I figured this out, it was nothing to do with the code above, but I thought I would answer my own question because someone else may run into the same issue. 我知道了这一点,与上面的代码无关,但是我想我会回答自己的问题,因为其他人可能会遇到相同的问题。

In the project I have an httpInterceptor set up that adds the authetentication token to each request for talking to my API. 在项目中,我设置了一个httpInterceptor ,它将身份验证令牌添加到每个与我的API对话的请求中。 This was what was causing the issue. 这就是导致问题的原因。 It so happened that I already defined a constant for my apiUrl , so I just updated the interceptor to check to make sure that the request url was my api before trying to append the token. 碰巧我已经为我的apiUrl定义了一个常量,因此在尝试附加令牌之前,我刚刚更新了拦截器以检查以确保请求url是我的api。 Like this: 像这样:

angular.module('widget.core').factory('authInterceptor', factory);

function factory($q, $location, $localStorage, apiUrl) {

    // The request function
    var request = function (config) {

        // If we are querying our API
        if (config.url.indexOf(apiUrl) > -1) {

            // Get our stored auth data
            var authData = angular.fromJson($localStorage.get('authorizationData'));

            // Set our headers to the request headers or a new object
            config.headers = config.headers || {};

            // If we have any auth data
            if (authData && authData.authenticated) {

                // Set our authorization header
                config.headers.Authorization = 'Bearer ' + authData.token;
            }
        }

        // Return our config
        return config;
    };

    return {
        request: request
    };
};

I hope that helps someone else. 希望对您有所帮助。 Took me hours to figure it out :/ 花了我几个小时才弄清楚:/

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

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