2017-09-11 7 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; 
     }); 
    }; 
}; 

を、この作業をする必要があります。私は郵便配達を使用し、まさにこの方法のようにそれを設定した場合、

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

:しかし、私は正しいAPIキーに入れていると私は、このメソッドを実行すると、私はこのエラーを取得する

私はこれを投稿する場合、それは問題なく動作します。 私はGoogleコンソールで自分のアプリケーションをチェックしましたが、これは無制限に設定されています。

誰もがこの問題に遭遇しましたか?誰もそれを解決する方法を知っていますか?

答えて

0

私はこれを考え出しましたが、上記のコードとは関係ありませんでしたが、他の人が同じ問題に遭遇する可能性があるため、自分の質問に答えると思いました。

プロジェクトでは、httpInterceptorが設定されています。これは、自分のAPIとの会話のための各リクエストにauthetenticationトークンを追加します。これが問題の原因となっていました。 私はすでに定数を定義していました。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 
    }; 
}; 

私は他人を助けることを望みます。それを理解するまで数時間かかりました:/

関連する問題