Skip to content

Commit b1c0d25

Browse files
committed
tests + docs
1 parent 10e4f18 commit b1c0d25

4 files changed

Lines changed: 195 additions & 91 deletions

File tree

packages/translate/README.md

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
- [API Documentation][gcloud-translate-docs]
77
- [Official Documentation][cloud-translate-docs]
88

9-
**An API key is required for Translate.** See [Identifying your application to Google][api-key-howto].
10-
119

1210
```sh
1311
$ npm install --save @google-cloud/translate
1412
```
1513
```js
1614
var translate = require('@google-cloud/translate')({
17-
key: 'API Key'
15+
projectId: 'grape-spaceship-123',
16+
keyFilename: '/path/to/keyfile.json'
1817
});
1918

2019
// Translate a string of text.
@@ -59,6 +58,71 @@ var translate = require('@google-cloud/translate')({
5958
```
6059

6160

61+
## Authentication
62+
63+
It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Google Cloud services.
64+
65+
### On Google Compute Engine
66+
67+
If you are running this client on Google Compute Engine, we handle authentication for you with no configuration. You just need to make sure that when you [set up the GCE instance][gce-how-to], you add the correct scopes for the APIs you want to access.
68+
69+
``` js
70+
// Authenticating on a global basis.
71+
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123'
72+
73+
var translate = require('@google-cloud/translate')({
74+
projectId: projectId
75+
});
76+
77+
// ...you're good to go!
78+
```
79+
80+
### With a Google Developers Service Account
81+
82+
If you are not running this client on Google Compute Engine, you need a Google Developers service account. To create a service account:
83+
84+
1. Visit the [Google Developers Console][dev-console].
85+
2. Create a new project or click on an existing project.
86+
3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services):
87+
* Google Translate API
88+
4. Navigate to **APIs & auth** > **Credentials** and then:
89+
* If you want to use a new service account, click on **Create new Client ID** and select **Service account**. After the account is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests.
90+
* If you want to generate a new key for an existing service account, click on **Generate new JSON key** and download the JSON key file.
91+
92+
``` js
93+
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123'
94+
95+
var translate = require('@google-cloud/translate')({
96+
projectId: projectId,
97+
98+
// The path to your key file:
99+
keyFilename: '/path/to/keyfile.json'
100+
101+
// Or the contents of the key file:
102+
credentials: require('./path/to/keyfile.json')
103+
});
104+
105+
// ...you're good to go!
106+
```
107+
108+
### With an API key
109+
110+
It's also possible to authenticate with an API key. To create an API key:
111+
112+
1. Visit the [Google Developers Console][dev-console].
113+
2. 2. Create a new project or click on an existing project.
114+
3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services):
115+
* Google Translate API
116+
4. Navigate to **APIs & auth** > **Credentials** and then click on **Create new Client ID** and select **API key**. You should then be presented with a pop-up containing your newly created key.
117+
118+
```js
119+
var translate = require('@google-cloud/translate')({
120+
key: 'API Key'
121+
});
122+
123+
// ...you're good to go!
124+
```
125+
62126
[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node/
63127
[api-key-howto]: https://cloud.google.com/translate/v2/using_rest#auth
64128
[gcloud-translate-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/translate

packages/translate/src/index.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var PKG = require('../package.json');
4141
* [Pricing](https://cloud.google.com/translate/v2/pricing.html) and
4242
* [FAQ](https://cloud.google.com/translate/v2/faq.html) pages for details.
4343
*
44-
* **An API key is required for Translate.** See
45-
* [Identifying your application to Google](https://cloud.google.com/translate/v2/using_rest#auth).
46-
*
4744
* @constructor
4845
* @alias module:translate
4946
*
@@ -53,7 +50,7 @@ var PKG = require('../package.json');
5350
* @throws {Error} If an API key is not provided.
5451
*
5552
* @param {object} options - [Configuration object](#/docs).
56-
* @param {string} options.key - An API key.
53+
* @param {string=} options.key - An API key.
5754
*
5855
* @example
5956
* //-
@@ -71,10 +68,6 @@ function Translate(options) {
7168
return new Translate(options);
7269
}
7370

74-
// if (!options.key) {
75-
// throw new Error('An API key is required to use the Translate API.');
76-
// }
77-
7871
var baseUrl = 'https://translation.googleapis.com/language/translate/v2';
7972

8073
if (process.env.GOOGLE_CLOUD_TRANSLATE_ENDPOINT) {
@@ -83,7 +76,6 @@ function Translate(options) {
8376
}
8477

8578
if (options.key) {
86-
this.baseUrl = baseUrl;
8779
this.options = options;
8880
this.key = options.key;
8981
}
@@ -173,10 +165,6 @@ Translate.prototype.detect = function(input, callback) {
173165
json: {
174166
q: input
175167
}
176-
// useQuerystring: true,
177-
// qs: {
178-
// q: input
179-
// }
180168
}, function(err, resp) {
181169
if (err) {
182170
callback(err, null, resp);
@@ -427,10 +415,10 @@ Translate.prototype.translate = function(input, options, callback) {
427415
};
428416

429417
/**
430-
* A custom request implementation. Requests to this API use an API key for an
431-
* application, not a bearer token from a service account. This means we skip
432-
* the `makeAuthenticatedRequest` portion of the typical request lifecycle, and
433-
* manually authenticate the request here.
418+
* A custom request implementation. Requests to this API may optionally use an
419+
* API key for an application, not a bearer token from a service account. This
420+
* means it is possible to skip the `makeAuthenticatedRequest` portion of the
421+
* typical request lifecycle, and manually authenticate the request here.
434422
*
435423
* @private
436424
*
@@ -439,12 +427,12 @@ Translate.prototype.translate = function(input, options, callback) {
439427
*/
440428
Translate.prototype.request = function(reqOpts, callback) {
441429
if (!this.key) {
442-
return common.Service.prototype.request.call(this, reqOpts, callback);
430+
common.Service.prototype.request.call(this, reqOpts, callback);
431+
return;
443432
}
444433

445434
reqOpts.uri = this.baseUrl + reqOpts.uri;
446435

447-
// @TODO see if we can send this via JSON
448436
reqOpts = extend(true, {}, reqOpts, {
449437
qs: {
450438
key: this.key

packages/translate/system-test/translate.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@ var prop = require('propprop');
2323
var env = require('../../../system-test/env.js');
2424
var Translate = require('../');
2525

26-
var API_KEY = process.env.GCLOUD_TESTS_API_KEY;
27-
28-
// Only run the tests if there is an API key to test with.
2926
describe('translate', function() {
30-
// (API_KEY ? describe : describe.skip)('translate', function() {
31-
// if (!API_KEY) {
32-
// // The test runner still executes this function, even if it is skipped.
33-
// return;
34-
// }
35-
3627
var translate = new Translate(extend({}, env));
3728

3829
describe('detecting language from input', function() {

0 commit comments

Comments
 (0)