· 5 years ago · Jan 29, 2021, 08:26 PM
1onst {google} = require('googleapis');
2
3// Each API may support multiple version. With this sample, we're getting
4// v3 of the blogger API, and using an API key to authenticate.
5const blogger = google.blogger({
6 version: 'v3',
7 auth: 'YOUR API KEY'
8});
9
10const params = {
11 blogId: '3213900'
12};
13
14// get the blog details
15blogger.blogs.get(params, (err, res) => {
16 if (err) {
17 console.error(err);
18 throw err;
19 }
20 console.log(`The blog url is ${res.data.url}`);
21});
22blogger.blogs.get(params)
23 .then(res => {
24 console.log(`The blog url is ${res.data.url}`);
25 })
26 .catch(error => {
27 console.error(error);
28 });
29async function runSample() {
30 const res = await blogger.blogs.get(params);
31 console.log(`The blog url is ${res.data.url}`);
32}
33runSample().catch(console.error);
34const {google} = require('googleapis');
35
36const oauth2Client = new google.auth.OAuth2(
37 YOUR_CLIENT_ID,
38 YOUR_CLIENT_SECRET,
39 YOUR_REDIRECT_URL
40);
41
42// generate a url that asks permissions for Blogger and Google Calendar scopes
43const scopes = [
44 'https://www.googleapis.com/auth/blogger',
45 'https://www.googleapis.com/auth/calendar'
46];
47
48const url = oauth2Client.generateAuthUrl({
49 // 'online' (default) or 'offline' (gets refresh_token)
50 access_type: 'offline',
51
52 // If you only need one scope you can pass it as a string
53 scope: scopes
54});
55GET /oauthcallback?code={authorizationCode}
56// This will provide an object with the access_token and refresh_token.
57// Save these somewhere safe so they can be used at a later time.
58const {tokens} = await oauth2Client.getToken(code)
59oauth2Client.setCredentials(tokens)
60oauth2Client.on('tokens', (tokens) => {
61 if (tokens.refresh_token) {
62 // store the refresh_token in my database!
63 console.log(tokens.refresh_token);
64 }
65 console.log(tokens.access_token);
66});
67oauth2Client.setCredentials({
68 refresh_token: `STORED_REFRESH_TOKEN`
69});
70const {google} = require('googleapis');
71const blogger = google.blogger_v3({
72 version: 'v3',
73 auth: 'YOUR_API_KEY' // specify your API key here
74});
75
76const params = {
77 blogId: '3213900'
78};
79
80async function main(params) {
81 const res = await blogger.blogs.get({blogId: params.blogId});
82 console.log(`${res.data.name} has ${res.data.posts.totalItems} posts! The blog url is ${res.data.url}`)
83};
84
85main().catch(console.error);
86const {google} = require('googleapis');
87const compute = google.compute('v1');
88
89async function main () {
90 const auth = new google.auth.GoogleAuth({
91 // Scopes can be specified either as an array or as a single, space-delimited string.
92 scopes: ['https://www.googleapis.com/auth/compute']
93 });
94 const authClient = await auth.getClient();
95
96 // obtain the current project Id
97 const project = await auth.getProjectId();
98
99 // Fetch the list of GCE zones within a project.
100 const res = await compute.zones.list({ project, auth: authClient });
101 console.log(res.data);
102}
103
104main().catch(console.error);
105$ GOOGLE_APPLICATION_CREDENTIALS=./your-secret-key.json node server.js
106const {google} = require('googleapis');
107
108const auth = new google.auth.GoogleAuth({
109 keyFile: '/path/to/your-secret-key.json',
110 scopes: ['https://www.googleapis.com/auth/cloud-platform'],
111});
112onst {google} = require('googleapis');
113
114const oauth2Client = new google.auth.OAuth2(
115 YOUR_CLIENT_ID,
116 YOUR_CLIENT_SECRET,
117 YOUR_REDIRECT_URL
118);
119
120// set auth as a global default
121google.options({
122 auth: oauth2Client
123});
124const {google} = require('googleapis');
125const oauth2Client = new google.auth.OAuth2(
126 YOUR_CLIENT_ID,
127 YOUR_CLIENT_SECRET,
128 YOUR_REDIRECT_URL
129);
130
131const drive = google.drive({
132 version: 'v2',
133 auth: oauth2Client
134});
135const res = await gmail.users.watch({
136 userId: 'me',
137 requestBody: {
138 // Replace with `projects/${PROJECT_ID}/topics/${TOPIC_NAME}`
139 topicName: `projects/el-gato/topics/gmail`
140 }
141});
142console.log(res.data);
143const drive = google.drive({
144 version: 'v3',
145 auth: oauth2Client
146});
147
148const res = await drive.files.create({
149 requestBody: {
150 name: 'Test',
151 mimeType: 'text/plain'
152 },
153 media: {
154 mimeType: 'text/plain',
155 body: 'Hello World'
156 }
157});
158const fs = require('fs');
159
160const drive = google.drive({
161 version: 'v3',
162 auth: oauth2Client
163});
164
165async function main() {
166 const res = await drive.files.create({
167 requestBody: {
168 name: 'testimage.png',
169 mimeType: 'image/png'
170 },
171 media: {
172 mimeType: 'image/png',
173 body: fs.createReadStream('awesome.png')
174 }
175 });
176 console.log(res.data);
177}
178
179main().catch(console.error);
180const {google} = require('googleapis');
181google.options({
182 // All requests made with this object will use these settings unless overridden.
183 timeout: 1000,
184 auth: auth
185});
186const {google} = require('googleapis');
187google.options({
188 // All requests from all services will contain the above query parameter
189 // unless overridden either in a service client or in individual API calls.
190 params: {
191 quotaUser: 'user123@example.com'
192 }
193});
194const blogger = google.blogger({
195 version: 'v3',
196 // All requests made with this service client will contain the
197 // blogId query parameter unless overridden in individual API calls.
198 params: {
199 blogId: '3213900'
200 }
201});
202
203// Calls with this drive client will NOT contain the blogId query parameter.
204const drive = google.drive('v3');
205const {google} = require('googleapis');
206const bigquery = google.bigquery('v2');
207
208async function main() {
209
210 // This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
211 // environment variables.
212 const auth = new google.auth.GoogleAuth({
213 scopes: ['https://www.googleapis.com/auth/cloud-platform']
214 });
215 const authClient = await auth.getClient();
216
217 const projectId = await auth.getProjectId();
218
219 const request = {
220 projectId,
221 datasetId: '<YOUR_DATASET_ID>',
222
223 // This is a "request-level" option
224 auth: authClient
225 };
226
227 const res = await bigquery.datasets.delete(request);
228 console.log(res.data);
229
230}
231
232main().catch(console.error);
233const res = await drive.files.export({
234 fileId: 'asxKJod9s79', // A Google Doc
235 mimeType: 'application/pdf'
236}, {
237 // Make sure we get the binary data
238 responseType: 'stream'
239});
240const {google} = require('googleapis');
241const apis = google.getSupportedAPIs();
242import { drive_v3 } from 'googleapis';
243const {google} = require('googleapis');
244google.options({
245 http2: true,
246});