2017-10-12 183 views
0

這是我的第一篇文章。我會盡力並完成。我正在使用谷歌oAuth在Meteor創建一個應用程序。我使用下列程序包:流星谷歌oAuth刷新accessToken無需重新登錄

accounts-ui 
accounts-google 
service-configuration 
google-config-ui 

在我login.js我:

Template.login.events({ 
    'click #googleLoginButton': function() { 
    Meteor.loginWithGoogle(
     { requestPermissions: ['email', 'profile'], 
     requestOfflineToken: 'true' 
     } 
    ); 
    } 
}); 

我得到了在蒙戈如下:

db.users.find({}).pretty(); 
{ 
    "_id" : "9TjGEjEj4ocFhwHtS", 
    "createdAt" : ISODate("2017-10-11T17:38:07.400Z"), 
    "services" : { 
     "google" : { 
      "accessToken" : "ya29.-REDACTED-T9z", 
      "idToken" : "eyJhbGcj-REDACTED-LTg", 
      "scope" : [ 
       "https://www.googleapis.com/auth/userinfo.email", 
       "https://www.googleapis.com/auth/userinfo.profile" 
      ], 
      "id" : "107113228066746203535", 
      "email" : "[email protected]", 
      "verified_email" : true, 
      "name" : "XXX XXX", 
      "given_name" : "XXX", 
      "family_name" : "XXX", 
      "picture" : "https://lh4.googleusercontent.com/.../photo.jpg", 
      "locale" : "en", 
      "gender" : "male", 
      "refreshToken" : "1/HG-REDACTED-oLq0USutc" 
     }, 
     "resume" : { 
      "loginTokens" : [ 
       { 
        "when" : ISODate("2017-10-11T21:47:38.471Z"), 
        "hashedToken" : "2lmOK-REDACTED-ptAyDnWo=" 
       } 
      ] 
     } 
    }, 
    "profile" : { 
     "name" : "XXX XXX" 
    } 
} 

在服務器/ init.js我有:(我通過settings.json傳遞google項目信息)

configureGoogle = function(config) { 
    ServiceConfiguration.configurations.remove({ 
     service: "google" 
    }); 

    ServiceConfiguration.configurations.insert({ 
     service: "google", 
     clientId: googleConfig.clientId, 
     secret: googleConfig.secret 
    }); 
    return; 
}; 

if (googleConfig) { 
    console.log('Got settings for google', googleConfig) 
    configureGoogle(googleConfig); 
} 

所以事情似乎正在起作用。我可以登錄/註銷,當我登錄時,我會獲得新的令牌。我在其他地方使用accessToken進行api呼叫。我遇到的問題是一小時後令牌過期並且API調用開始失敗。

我想刷新accessToken就在它過期之前,似乎無法弄清楚如何。我還想在24小時後強制註銷,因此令牌不會永久刷新。

任何援助,如果讚賞。

+1

我覺得這已經回答了: https://stackoverflow.com/questions/32764769/meteor-accounts-google-token-expires –

+1

的可能的複製[流星賬戶,谷歌標記過期](https://開頭stackoverflow.com/questions/32764769/meteor-accounts-google-token-expires) –

回答

1

更新#1

得益於以下德里克·布朗指着我在正確的方向。這讓我尋找流星谷歌API。我發現這一個:percolate:google-api

,並根據它的文檔它做什麼,我一直在尋找:

如果用戶的訪問令牌已過期,它會透明地調用exchangeRefreshToken方法來獲得一個新的刷新令牌。

然後我跑進其中expiresAt Didnt在我的MongoDB,事情並沒有工作存在的錯誤。然後我發現這個職位:Google-API這表明:

服務器/ publish.js

Meteor.publish(null, function() { 
    return Meteor.users.find(this.userId, { fields: { 
    'services.google.accessToken': 1, 
    'services.google.expiresAt': 1 
    }}); 
}); 

我還由從上述代碼的一個變化: 客戶機/ login.js

Template.login.events({ 
    'click #googleLoginButton': function() { 
    Meteor.loginWithGoogle(
     { requestPermissions: ['email', 'profile'], 
     requestOfflineToken: 'true', 
     forceApprovalPrompt: 'true' //<==== This is the change 
     } 
    ); 
    } 
}); 

這增加沒有額外的代碼改變,這增加了expiresAt並填充它。

我現在正在做最後的測試。我會更新。

+0

我面臨同樣的問題。增加forceApprovalPrompt沒有幫助。你有沒有發現關於這個問題的其他事情? –