2017-07-28 58 views
0

我目前正在嘗試設置發送對象給谷歌的S2AP。 谷歌建議簽署JWTs像this用google-auth-library-ruby編碼JWT 0.9+

jwt = { 
    "iss"=> SERVICE_ACCOUNT_EMAIL_ADDRESS, 
    "aud" => "google", 
    "typ" => "savetoandroidpay", 
    "iat"=> Time.now.utc.to_i, 
    "payload" => { 
    "loyaltyObjects" => [], #Loyalty objects 
    "offerObjects" => [], #Offer objects 
    "loyaltyClasses" => [], #Loyalty classes 
    "offerClasses" => [] #Offer classes 
    }, 
    "origins"=> ['http://baconrista.com', 'https://baconrista.com'] 
} 
private_key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PRIVATE_KEY, 'notasecret') 
jwtEncoded = JWT.encode(jwt, private_key, "RS256") 

然而,由於0.8不再是標準,而不再支持Google::APIClient,我無法找出如何簽署JWT與來自計算所提供的JSON文件引擎服務帳戶。

我目前已經設置了所有其他設置,以生成訪問令牌來與使用OAuth2身份驗證的Google API進行對話,但無法讓我終身瞭解如何對這些JWT進行編碼。

供參考,這是你如何從谷歌獲得訪問令牌與紅寶石gem

compute = Google::Apis::ComputeV1::ComputeService.new 
File.open(CONFIG[:GOOGLE_APPLICATION_CREDENTIALS], "r") do |json_io| 
    compute.authorization = Google::Auth::DefaultCredentials.make_creds(scope: 'https://www.googleapis.com/auth/wallet_object.issuer', json_key_io: json_io) 
end 
TOKEN = compute.authorization.fetch_access_token! 

回答

0

爲了解決缺乏使用較新的服務(這是不完全完成簽訂智威湯遜的能力)我最終拉動PRIVATE_KEY出他們有JSON密鑰文件,然後重新登錄它像這樣:

def create_signed_JWT(obj, type) 
    JSON.parse(File.open(keyfile.path, 'rb').read) 
    jwt = { 
    iss: credentials['client_email'], 
    aud: "google", 
    typ: "savetoandroidpay", 
    iat: Time.now.utc.to_i, 
    payload: { 
     "#{type}Objects": obj 
    }, 
    origins: [ "https://#{CONFIG[:domain]}", "http://#{CONFIG[:domain]}" ] 
    } 
    JWT.encode(jwt, OpenSSL::PKey::RSA.new(credentials['private_key'], nil), "RS256") 
end 

obj是JSON格式的報價/忠誠對象, 類型爲對象

類型