2017-01-13 6 views
0

보안 된 API 키를 생성하려고하지만 올바르게 생성되지 않은 것 같습니다. API 문서를 따랐습니다. Algolia가 잘못된 보안 API 키를 생성합니다.

내가

var algoliasearch = require('algoliasearch'); 

var admin_client = algoliasearch('APP', 'ADMIN_KEY'); 
var search_client = algoliasearch('APP', 'ONLY_SEARCH_KEY'); 

var admin_index = admin_client.initIndex('INDEX'); 
var search_index = search_client.initIndex('INDEX'); 

admin_index.search('dav', (err, content) => { console.log(err, content) }); 
//------------------CONSOLE------------------------------- 
null { hits: 
[ { firstname: 'David', 
    lastname: 'De Anda', 
    _tags: [Object], 
    objectID: '2', 
    _highlightResult: [Object] } ], 
nbHits: 1, 
page: 0, 
nbPages: 1, 
hitsPerPage: 20, 
processingTimeMS: 1, 
query: 'dav', 
params: 'query=dav' } 
//------------------------------------------------- 

search_index.search('dav', (err, content) => { console.log(err, content) }); 

//------------------CONSOLE------------------------------- 
null { hits: 
[ { firstname: 'David', 
    lastname: 'De Anda', 
    _tags: [Object], 
    objectID: '2', 
    _highlightResult: [Object] } ], 
nbHits: 1, 
page: 0, 
nbPages: 1, 
hitsPerPage: 20, 
processingTimeMS: 1, 
query: 'dav', 
params: 'query=dav' } 
//------------------------------------------------- 

모든 것이 지금까지 작동하는 것 같다 뭘하는지입니다하지만 지금은 일부 보안 API-키

var valid_until = Math.floor(Date.now()/1000) + 3600 

var from_admin_api_key = admin_client.generateSecuredApiKey('from_admin', {validUntil: valid_until}); 
var from_search_api_key = search_client.generateSecuredApiKey('from_search', {validUntil: valid_until}); 

var sub_admin_client = algoliasearch('APP', from_admin_api_key); 
var sub_search_client = algoliasearch('APP', from_search_api_key); 

var sub_admin_index = sub_admin_client.initIndex('INDEX'); 
var sub_search_index = sub_search_client.initIndex('INDEX'); 

sub_admin_index.search('dav', (err, content) => { console.log(err, content) }); 
//------------------CONSOLE------------------------------- 
{ Error 
    at success (/app/node_modules/algoliasearch/src/AlgoliaSearchCore.js:334:32) 
    at process._tickDomainCallback (internal/process/next_tick.js:129:7) 
    name: 'AlgoliaSearchError', 
    message: 'Invalid Application-ID or API key', 
    debugData: 
    [ { currentHost: 'https://ge24e6css9-dsn.algolia.net', 
     headers: [Object], 
     content: '{"params":"query=dav"}', 
     contentLength: 22, 
     method: 'POST', 
     timeouts: [Object], 
     url: '/1/indexes/INDEX/query', 
     startTime: 2017-01-13T17:46:42.519Z, 
     endTime: 2017-01-13T17:46:44.038Z, 
     duration: 1519, 
     statusCode: 403 } ], 
    statusCode: 403 } undefined 
//------------------------------------------------- 

sub_search_index.search('dav', (err, content) => { console.log(err, content) }); 

//------------------CONSOLE------------------------------- 
{ Error 
    at success (/app/node_modules/algoliasearch/src/AlgoliaSearchCore.js:334:32) 
    at process._tickDomainCallback (internal/process/next_tick.js:129:7) 
    name: 'AlgoliaSearchError', 
    message: 'Invalid Application-ID or API key', 
    debugData: 
    [ { currentHost: 'https://ge24e6css9-dsn.algolia.net', 
     headers: [Object], 
     content: '{"params":"query=dav"}', 
     contentLength: 22, 
     method: 'POST', 
     timeouts: [Object], 
     url: '/1/indexes/INDEX/query', 
     startTime: 2017-01-13T17:46:42.519Z, 
     endTime: 2017-01-13T17:46:44.038Z, 
     duration: 1519, 
     statusCode: 403 } ], 
    statusCode: 403 } undefined 
//------------------------------------------------- 

답변

0

내가 generateSecuredApiKey 첫 번째 매개 변수를 오해 생성 할 그 사실 원점 API 키였습니다.

그래서 올바른 코드는 ADMIN_KEY 작동하지 않습니다

var from_admin_api_key = admin_client.generateSecuredApiKey('ADMIN_KEY', {validUntil: valid_until}); 
var from_search_api_key = search_client.generateSecuredApiKey('ONLY_SEARCH_KEY', {validUntil: valid_until}); 

그리고 물론 생성 된 키 형태가 될 것입니다.