2017-09-11 1 views
2

현재 Google URL 단축키로 문제가 있습니다. 나는이 서비스를 설정 한 : 지금까지 내가 말할 수있는angularjs 및 googles URL 단축키

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; 
     }); 
    }; 
}; 

을이 작동합니다. 나는 올바른 API 키에 넣어 가지고 내가이 방법을 실행하면이 오류 얻을 : 나는 우체부를 사용하여 정확히이 방법처럼 설정 한 경우,

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

을하지만 :

나는이를 게시 할 때이 문제없이 작동합니다. Google 콘솔에서 내 애플리케이션을 확인했지만 제한되지 않도록 설정되었습니다.

이전에이 문제를 접한 사람이 있습니까? 누구든지 그것을 해결하는 방법을 알고 있습니까?

답변

0

나는 이것을 알아 냈다. 위의 코드와 아무런 관련이 없었다.하지만 다른 사람이 같은 문제에 부딪 힐 수도 있기 때문에 나는 내 자신의 질문에 대답 할 것이라고 생각했다.

프로젝트에서 필자는 httpInterceptor을 설정하여 API에 대한 각 요청에 자동 형성 토큰을 추가합니다. 이것이 문제의 원인이었습니다. 나는 이미 내 상수를 정의했다. apiUrl 그래서 토큰을 추가하기 전에 요청 URL이 내 API 였는지 확인하기 위해 요격기를 업데이트했다. 이처럼 :

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 
    }; 
}; 

는 그 다른 사람을 도움이되기를 바랍니다. 알아내는 데 몇 시간 걸렸습니다./