2014-04-20 123 views
4

我試圖在節點上使用YouTube數據API V3,試圖提供一個經過身份驗證的代理服務器,用於執行視頻搜索。由於該服務將在服務器上運行,因此我創建了一個服務帳戶。在Nodejs中使用Google API進行服務身份驗證

這導致在下載密鑰文件及以下信息:

我有JWTs的一個很基本的瞭解,但對OAuth2 page服務部建議,你應該使用他們的圖書館之一做它,而不是自己實施它。但是,他們不會在supported libraries中列出節點庫。

多一點挖掘,我碰到了Google API Nodejs客戶端:https://github.com/google/google-api-nodejs-client,聲稱受谷歌支持,並支持OAuth2開箱即用。

文檔非常小,身份驗證的示例涉及將URL打印到終端中,然後在瀏覽器中訪問它以生成令牌。我周圍的源一挖,看看它是否支持智威湯遜和它似乎有一個JWTClient內置的。

/** 
* @constructor 
* JWT service account credentials. 
* 
* Retrieve access token using gapitoken. 
* 
* @param {string=} email service account email address. 
* @param {string=} keyFile path to private key file. 
* @param {array=} scopes list of requested scopes. 
* @param {string=} subject impersonated account's email address. 
* 
*/ 
function JWT(email, keyFile, key, scopes, subject) { 
    JWT.super_.call(this); 
    this.email = email; 
    this.subject = subject; 
    this.keyFile = keyFile; 
    this.key = key; 
    this.scopes = scopes; 
    this.GAPI = GAPI; 
} 

構造上面這個文檔註釋似乎這裏是唯一的解釋,我仍然不確定如何使用這個功能。

我假設:

  • email是您的開發者帳戶
  • keyFile的電子郵件地址的路徑私鑰
  • key的私鑰? (如果是這樣,爲什麼還包括路徑?)
  • scopes是您想要訪問的API示波器的數組
  • subject ???

以前有沒有人使用過這個服務,或者能夠對這裏的差異稍微解釋一下?

作爲一個參考,這必須是服務器端,因爲我需要身份驗證的請求是自治的,我不能只使用未經過身份驗證的請求,因爲我超過了我的每日補貼200請求(或類似的東西)。

回答

11

我已經設法拼湊出了一個可行的解決方案,所以我將在此留給任何其他未來有類似問題的人。

回購中有一個JWT示例,它更詳細地顯示瞭如何使用JWT對象的構造函數。

https://github.com/google/google-api-nodejs-client/blob/master/examples/jwt.js

這裏的文檔註釋的稍作修改的版本。

/** 
* @constructor 
* JWT service account credentials. 
* 
* Retrieve access token using gapitoken. 
* 
* @param {string=} email service account email address from developer console. 
* @param {string=} keyFile absolute path to private key file. 
* @param {string=} key the contents of the key if you are loading it yourself (optional) 
* @param {array=} scopes list of requested scopes. 
* @param {string=} subject impersonated account's email address (optional). 
* 
*/ 

而完成的代碼看起來像這樣

// Create the JWT client 
var authClient = new googleapis.auth.JWT(email, keypath, key, 
    ['https://www.googleapis.com/auth/youtube']); 

// Authorize it to produce an access token 
authClient.authorize(function(err, tokens) { 
    if(err) throw err; 

    // Use discovery to get the youtube api 
    googleapis.discover('youtube', 'v3') 
    .execute(function(err, client) { 

    // Create a search 
    var search = client.youtube.search.list({ 
     q: '<query string>', 
     part: 'snippet', 
     maxResults: 50 
    }); 

    // Authenticate with current auth client 
    search.authClient = authClient; 

    // Hey presto! 
    search.execute(function(err, results) { 
     if(err) throw err; 
     console.log(results.items); 
    }); 

    }); 

}); 

此代碼示例是一個爛攤子,但它足以讓你的想法。根據該示例,你應該能夠做到:

client.youtube.search.list({ 
... 
}) 
.withAuth(authClient) 
.execute(...); 

但由於某些原因的withAuth方法是不存在的。我們花了一些時間來研究它做了什麼,並且據我所知,如果您手動設置authClient屬性,它會正常工作,如上所示。

作爲一個側面說明,err參數不是錯誤,但POJO並按上述方式拋出它實際上不起作用。除非你很高興看到[Object object]作爲你的調試信息。

希望圖書館能儘快得到一些適當的文獻資料,這樣的問題會被解決。

+0

謝謝你的例子。如果我沒有提供'key',我遇到了一個問題,它將'scopes'參數視爲'keys',然後將範圍留空。你只是省略了「鑰匙」還是我完全錯過了某些東西? – imjared

+0

我在一個階段遇到了類似問題,文檔似乎建議您可以將明文中的密鑰作爲參數傳遞,或者傳遞給密鑰文件的路徑。它並沒有解釋爲什麼,但似乎給我一個錯誤。我結束了通過兩個參數,它的工作(哈克,我知道)。 –

+0

'''authClient = new googleapis.auth.JWT(config.jwt.email,config.jwt.keyPath,config.jwt.key,[https://www.googleapis.com/auth/youtube']); ''' –

相關問題