私のコードで何を使用すべきか、そして約束をどのように使うべきかを理解することはできません。私の$ httpの呼び出しで。
私は、次のような機能
は最初、私は
$scope.getAcessToken()
を呼びたいと私はアクセストークンを得れば、私は
$scope.getDataSets(lastSaved, cTime, accessToken)
二つの機能があり、次の呼び出しを行うことになる持っています
$scope.getAcessToken = function()
{
alert("inside getAcessToken function");
refreshToken = localStorage.getItem("refreshToken");
if(refreshToken)
{
$http({
method: "post", url: "https://accounts.google.com/o/oauth2/token",
data: "client_secret=" + clientSecret + "&grant_type=refresh_token" + "&refresh_token="+ refreshToken + "&client_id=" + clientId
})
.success(function(data){
accessToken = data.access_token;
})
.error(function(data,status){
alert("ERROR: " + JSON.stringify(data) + status);
});
}
else
{
$scope.firstTimeAuth();
}
return accessToken;
}
と
$scope.getDataSets = function(startTime, endTime, accessToken,)
{
$scope.a = "inside dataSets function";
$url = "https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:com.google.android.gms:estimated_steps/datasets/" + startTime +"000000"+"-"+ endTime + "000000";
alert("acess token is " + accessToken);
if(accessToken != "")
{
$http({method: 'GET', url: $url,
headers: {'Authorization': 'OAuth ' + accessToken},
})
.success(function(response){
alert("inside success block datasets");
$scope.handleResponse(response);
})
.error(function(response) {
alert("Something went wrong" + JSON.stringify(response));
});
}
else
{
alert("no access token received");
}
}
私は実際にそれを防ぐためにどのようにアクセストークン を取得する前にデータセットが実行されますを取得? EDIT 1:
$scope.firstTimeAuth = function(callback) {
var ref = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=https://www.googleapis.com/auth/fitness.activity.write &approval_prompt=force&response_type=code&access_type=offline', '_blank', 'location=no');
ref.addEventListener('loadstart', function(event) {
if((event.url).startsWith("http://localhost/callback")) {
requestToken = (event.url).split("code=")[1];
$http({
method: "post", url: "https://accounts.google.com/o/oauth2/token",
data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=http://localhost/callback" + "&grant_type=authorization_code" + "&code=" + requestToken
})
.success(function(data) {
accessToken = data.access_token;
refreshToken = data.refresh_token;
if(typeof(Storage) != "undefined") {
localStorage.setItem("refreshToken", refreshToken);
alert(localStorage.getItem("refreshToken"));
} else {
alert("Sorry, your browser does not support Web Storage...");
}
//$location.path("/secure");
})
.error(function(data, status) {
alert("ERROR: " + data);
});
ref.close();
}
});
callback();
}
:あなたの要件ごとに下記($q doc link)
は、最も可能性の高い実装コードです'firstTimeAuth'関数 – phuzi
もう一つの注意点として、' 'success''と' '''''''''''約束メソッドを使用しないようにするべきです(https://docs.angularjs.org/api/ng/ service/$ http#deprecation-notice) – phuzi
大丈夫です。私はこれにはかなり新しいです。だから、成功のブロックの代わりに何を使うべきかを確信していませんでした。 初めての認証はgetaccessTokenに似た関数で、$ httpも呼び出すことができます。とにかく病気を追加する – Shravan