2015-01-09 5 views
1

이온 (각도)이있는 하이브리드 모바일 앱을 만들려고합니다. 이 응용 프로그램의 경우 oAuth 호출을 만들고 있는데 이는 jQuery 종속이므로 oAuth와 my 및 my app의 스크립트뿐만 아니라 두 라이브러리를 모두로드해야합니다.

$ionicPlatform.ready(function(){ 
    oAuthProcess.authorize({ 
    client_id: 'client', 
    client_secret: 'secret', 
    scope: 'scope', 
    redirect_uri: 'fake url' 
    }).done(function(data){ 
    localStorage.setItem('accessToken', data.access_token); 
    localStorage.setItem('refreshToken', data.refresh_token); 
    var accessToken = localStorage.getItem("accessToken"); 
    alert(accessToken); 
    }).fail(function(data){alert(data.error);}); 
}); 

oAuthProcess 기능은 아래처럼 보이는 auth.js 파일에 아래 그림과 같이

<script src="lib/ionic/js/ionic.bundle.js"></script> 
<script src="cordova.js"></script> 
<script src="js/jquery-1.11.1.min.js"></script> 
<script src="js/auth.js"></script> 
<script src="js/app.js"></script> 

인증을 시작하기 위해 호출

는 app.js의 이온 준비 이벤트에서 이루어집니다. 이 인증을 수행 할 수 inAppBrowser를 열고 다음 호출 API를 할 수 있도록 응용 프로그램에 액세스 토큰을 반환을 닫아야합니다 :

var oAuthProcess = { 
    authorize: function(options) { 
    var deferred = $.Deferred(); 
    var authUrl = 'some url' + $.param({ 
    client_id: options.client_id, 
    redirect_uri: options.redirect_uri, 
    response_type: 'code', 
    scope: options.scope 
    }); 
    //Open inAppBrowser with authUrl 
    var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no'); 
    authWindow.addEventListener('loadstart', function(e) { 
    var url = '' + e.url + ''; 
    //Upon opening in     
    var code = url.match(/\?code=(.+)$/); 
    var error = url.match(/\?error=(.+)$/); 
    if (code != null || error != null) { 
     authWindow.close(); 
    } 
    if (code) { 
     $http({ 
     method: 'POST', 
     url: 'some url', 
     data: {code: code[1], client_id: options.client_id, client_secret: options.client_secret, redirect_uri: options.redirect_uri, grant_type: 'authorization_code'} 
     }).success(function(data) { 
     deferred.resolve(data); 
     }).error(function(data){ 
     deferred.reject(response.responseJSON); 
     }); 
    } else if (error) { 
     deferred.reject({ 
     error: error[1] 
     }); 
    } 
    }); 
    return deferred.promise(); 
} 

을};

응용 프로그램은 inAppBrowser를로드하고 토큰을 만들 수 있지만 inAppBrowser가 닫힌 후에 토큰이 응용 프로그램으로 돌아 가지 않도록하는 다음과 같은 오류가 발생합니다. 48 :

2015년 1월 9일 16

04.299을 myApp [2146 : 483400] 성공 callbackId 오류 : InAppBrowser85303841 : ReferenceError가 : 변수를 찾을 수 없습니다 : $ HTTP

어떤 도움이 또는 다른 방법을 해결한다 매우 감사하겠습니다.

미리 감사드립니다. 당신이 준 예를 가정

+0

'$ http'는 전역 객체에 노출 된 것이 아닙니다. 의존성 삽입을 사용하여 액세스해야합니다. 당신이'$ injector'에서'$ http'에 접근 할 필요가있는 글로벌 컨텍스트에서 이것을하고 있습니다. – PSL

+0

@PSL 빠른 응답에 감사드립니다. abit에 대해 더 설명해 주시겠습니까? 어떻게 할 수 있습니까? 저는 매우 새로운 각도입니다 – Peeks

+1

각도가로드되어 있으면'var $ http = angle.injector ([ 'ng']). get ('$ http')'을 실행하여 http 인스턴스를 얻을 수 있습니다. 또한 익명의 이벤트 처리기에서 약속을 반환하는 지점을 이해하지 못합니다. – PSL

답변

2

차라리 코멘트보다는 당신에게 코드 예제를 표시 할 수 있도록 난 그냥 여기 입력거야 ...

전체 auth.js 파일, 그래서 그 예 PSL이 준 추가 파일은 이제 다음과 같이 보입니다 :

var $http = angular.injector(['ng']).get('$http'); 
var oAuthProcess = { 
    authorize: function(options) { 
    var deferred = $.Deferred(); 
    var authUrl = 'some url' + $.param({ 
    client_id: options.client_id, 
    redirect_uri: options.redirect_uri, 
    response_type: 'code', 
    scope: options.scope 
    }); 
    //Open inAppBrowser with authUrl 
    var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no'); 
    authWindow.addEventListener('loadstart', function(e) { 
    var url = '' + e.url + ''; 
    //Upon opening in     
    var code = url.match(/\?code=(.+)$/); 
    var error = url.match(/\?error=(.+)$/); 
    if (code != null || error != null) { 
     authWindow.close(); 
    } 
    if (code) { 
     $http({ 
     method: 'POST', 
     url: 'some url', 
     data: {code: code[1], client_id: options.client_id, client_secret: options.client_secret, redirect_uri: options.redirect_uri, grant_type: 'authorization_code'} 
     }).success(function(data) { 
     deferred.resolve(data); 
     }).error(function(data){ 
     deferred.reject(response.responseJSON); 
     }); 
    } else if (error) { 
     deferred.reject({ 
     error: error[1] 
     }); 
    } 
    }); 
    return deferred.promise(); 
} 
+0

불행히도이 문제가 해결되지 않았습니다. – Peeks

+0

$ http의 정의되지 않은 오류가 발생합니까? –

관련 문제