1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
import endpoints from './endpoints';
import { Context } from 'cafy';
import config from '../../config';
const basicErrors = {
'400': {
'INVALID_PARAM': {
value: {
error: {
message: 'Invalid param.',
code: 'INVALID_PARAM',
id: '3d81ceae-475f-4600-b2a8-2bc116157532',
}
}
}
},
'401': {
'CREDENTIAL_REQUIRED': {
value: {
error: {
message: 'Credential required.',
code: 'CREDENTIAL_REQUIRED',
id: '1384574d-a912-4b81-8601-c7b1c4085df1',
}
}
}
},
'403': {
'AUTHENTICATION_FAILED': {
value: {
error: {
message: 'Authentication failed. Please ensure your token is correct.',
code: 'AUTHENTICATION_FAILED',
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14',
}
}
}
},
'418': {
'I_AM_AI': {
value: {
error: {
message: 'You sent a request to Ai-chan, Misskey\'s showgirl, instead of the server.',
code: 'I_AM_AI',
id: '60c46cd1-f23a-46b1-bebe-5d2b73951a84',
}
}
}
},
'429': {
'RATE_LIMIT_EXCEEDED': {
value: {
error: {
message: 'Rate limit exceeded. Please try again later.',
code: 'RATE_LIMIT_EXCEEDED',
id: 'd5826d14-3982-4d2e-8011-b9e9f02499ef',
}
}
}
},
'500': {
'INTERNAL_ERROR': {
value: {
error: {
message: 'Internal error occurred. Please contact us if the error persists.',
code: 'INTERNAL_ERROR',
id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
}
}
}
}
};
const schemas = {
Error: {
type: 'object',
properties: {
error: {
type: 'object',
description: 'An error object.',
properties: {
code: {
type: 'string',
description: 'An error code.',
},
message: {
type: 'string',
description: 'An error message.',
},
id: {
type: 'string',
format: 'uuid',
description: 'An error ID. This ID is static.',
}
},
required: ['code', 'id', 'message']
},
},
required: ['error']
},
User: {
type: 'object',
properties: {
id: {
type: 'string',
format: 'id',
description: 'The unique identifier for this User.'
},
username: {
type: 'string',
description: 'The screen name, handle, or alias that this user identifies themselves with.',
example: 'ai'
},
name: {
type: 'string',
nullable: true,
description: 'The name of the user, as they’ve defined it.',
example: '藍'
},
host: {
type: 'string',
nullable: true,
example: 'misskey.example.com'
},
description: {
type: 'string',
nullable: true,
description: 'The user-defined UTF-8 string describing their account.',
example: 'Hi masters, I am Ai!'
},
createdAt: {
type: 'string',
format: 'date-time',
description: 'The date that the user account was created on Misskey.'
},
followersCount: {
type: 'number',
description: 'The number of followers this account currently has.'
},
followingCount: {
type: 'number',
description: 'The number of users this account is following.'
},
notesCount: {
type: 'number',
description: 'The number of Notes (including renotes) issued by the user.'
},
isBot: {
type: 'boolean',
description: 'Whether this account is a bot.'
},
isCat: {
type: 'boolean',
description: 'Whether this account is a cat.'
},
isAdmin: {
type: 'boolean',
description: 'Whether this account is the admin.'
},
isVerified: {
type: 'boolean'
},
isLocked: {
type: 'boolean'
},
},
required: ['id', 'name', 'username', 'createdAt']
},
Note: {
type: 'object',
properties: {
id: {
type: 'string',
format: 'id',
description: 'The unique identifier for this Note.'
},
createdAt: {
type: 'string',
format: 'date-time',
description: 'The date that the Note was created on Misskey.'
},
text: {
type: 'string'
},
cw: {
type: 'string'
},
userId: {
type: 'string',
format: 'id',
},
user: {
$ref: '#/components/schemas/User'
},
replyId: {
type: 'string',
format: 'id',
},
renoteId: {
type: 'string',
format: 'id',
},
reply: {
$ref: '#/components/schemas/Note'
},
renote: {
$ref: '#/components/schemas/Note'
},
viaMobile: {
type: 'boolean'
},
visibility: {
type: 'string'
},
},
required: ['id', 'userId', 'createdAt']
},
DriveFile: {
type: 'object',
properties: {
id: {
type: 'string',
format: 'id',
description: 'The unique identifier for this Drive file.'
},
createdAt: {
type: 'string',
format: 'date-time',
description: 'The date that the Drive file was created on Misskey.'
},
name: {
type: 'string',
description: 'The file name with extension.',
example: 'lenna.jpg'
},
type: {
type: 'string',
description: 'The MIME type of this Drive file.',
example: 'image/jpeg'
},
md5: {
type: 'string',
format: 'md5',
description: 'The MD5 hash of this Drive file.',
example: '15eca7fba0480996e2245f5185bf39f2'
},
datasize: {
type: 'number',
description: 'The size of this Drive file. (bytes)',
example: 51469
},
folderId: {
type: 'string',
format: 'id',
nullable: true,
description: 'The parent folder ID of this Drive file.',
},
isSensitive: {
type: 'boolean',
description: 'Whether this Drive file is sensitive.',
},
},
required: ['id', 'createdAt', 'name', 'type', 'datasize', 'md5']
}
};
const desc = `
## Usage
APIはすべてPOSTでリクエスト/レスポンスともにJSON形式です。
一部のAPIは認証情報(アクセストークン)が必要です。リクエストの際に\`i\`というパラメータでアクセストークンを添付してください。
### アクセストークンを取得する
#### 自分のアカウントのアクセストークンを取得する
「設定 > API」で、自分のアクセストークンを取得できます。
> アカウントを不正利用される可能性があるため、このトークンは第三者に教えないでください(アプリなどにも入力しないでください)。
### アプリケーションとしてアクセストークンを取得する
直接ユーザーのアクセストークンをアプリケーションが扱うのは危険なので、
アプリケーションからAPIを利用する際には、アプリケーションとアプリケーションを利用するユーザーが結び付けられた専用のアクセストークンをMisskeyに発行してもらいます。
#### 1.アプリケーションを登録する
まず、あなたのアプリケーションやWebサービス(以後、あなたのアプリと呼びます)をMisskeyに登録します。
[デベロッパーセンター](/dev)にアクセスし、「アプリ > アプリ作成」からアプリを作成してください。
フォームの記入欄の説明は以下の通りです:
| 名前 | 説明 |
|---|---|
| アプリケーション名 | あなたのアプリの名称。 |
| アプリの概要 | あなたのアプリの簡単な説明や紹介。 |
| コールバックURL | ユーザーが後述する認証フォームで認証を終えた際にリダイレクトするURLを設定できます。あなたのアプリがWebサービスである場合に有用です。 |
| 権限 | あなたのアプリが要求する権限。ここで要求した機能だけがAPIからアクセスできます。 |
登録が済むとあなたのアプリのシークレットキーが入手できます。このシークレットキーは後で使用します。
> アプリに成りすまされる可能性があるため、極力このシークレットキーは公開しないようにしてください。</p>
#### 2.ユーザーに認証させる
アプリを使ってもらうには、ユーザーにアカウントへのアクセスの許可をもらう必要があります。
認証セッションを開始するには、%API_URL%/auth/session/generate へパラメータに appSecret としてシークレットキーを含めたリクエストを送信します。
リクエスト形式はJSONで、メソッドはPOSTです。
レスポンスとして認証セッションのトークンや認証フォームのURLが取得できるので、認証フォームのURLをブラウザで表示し、ユーザーにフォームを提示してください。
あなたのアプリがコールバックURLを設定している場合、
ユーザーがあなたのアプリの連携を許可すると設定しているコールバックURLに token という名前でセッションのトークンが含まれたクエリを付けてリダイレクトします。
あなたのアプリがコールバックURLを設定していない場合、ユーザーがあなたのアプリの連携を許可したことを(何らかの方法で(たとえばボタンを押させるなど))確認出来るようにしてください。
#### 3.ユーザートークンを取得する
ユーザーが連携を許可したら、%API_URL%/auth/session/userkey へ次のパラメータを含むリクエストを送信します:
| 名前 | 型 | 説明 |
|---|---|---|
| appSecret | string | アプリのシークレットキー |
| token | string | セッションのトークン |
上手くいけば、認証したユーザーのユーザートークンがレスポンスとして取得できます。おめでとうございます!
ユーザートークンが取得できたら、「ユーザーのユーザートークン+あなたのアプリのシークレットキーをsha256したもの」をアクセストークンとして、APIにリクエストできます。
アクセストークンの生成方法を擬似コードで表すと次のようになります:
<pre><code>const i = sha256(userToken + secretKey);</code></pre>
`;
export function genOpenapiSpec(lang = 'ja-JP') {
const spec = {
openapi: '3.0.0',
info: {
version: 'v1',
title: 'Misskey API',
description: '**Misskey is a decentralized microblogging platform.**\n\n' + desc,
'x-logo': { url: '/assets/api-doc.png' }
},
externalDocs: {
description: 'Repository',
url: 'https://github.com/syuilo/misskey'
},
servers: [{
url: config.api_url
}],
paths: {} as any,
components: {
schemas: schemas,
securitySchemes: {
ApiKeyAuth: {
type: 'apiKey',
in: 'body',
name: 'i'
}
}
}
};
function genProps(props: { [key: string]: Context & { desc: any, default: any }; }) {
const properties = {} as any;
const kvs = Object.entries(props);
for (const kv of kvs) {
properties[kv[0]] = genProp(kv[1], kv[1].desc, kv[1].default);
}
return properties;
}
function genProp(param: Context, desc?: string, _default?: any): any {
const required = param.name === 'Object' ? (param as any).props ? Object.entries((param as any).props).filter(([k, v]: any) => !v.isOptional).map(([k, v]) => k) : [] : [];
return {
description: desc,
default: _default,
...(_default ? { default: _default } : {}),
type: param.name === 'ID' ? 'string' : param.name.toLowerCase(),
...(param.name === 'ID' ? { example: 'xxxxxxxxxxxxxxxxxxxxxxxx', format: 'id' } : {}),
nullable: param.isNullable,
...(param.name === 'String' ? {
...((param as any).enum ? { enum: (param as any).enum } : {}),
...((param as any).minLength ? { minLength: (param as any).minLength } : {}),
...((param as any).maxLength ? { maxLength: (param as any).maxLength } : {}),
} : {}),
...(param.name === 'Number' ? {
...((param as any).minimum ? { minimum: (param as any).minimum } : {}),
...((param as any).maximum ? { maximum: (param as any).maximum } : {}),
} : {}),
...(param.name === 'Object' ? {
...(required.length > 0 ? { required } : {}),
properties: (param as any).props ? genProps((param as any).props) : {}
} : {}),
...(param.name === 'Array' ? {
items: (param as any).ctx ? genProp((param as any).ctx) : {}
} : {})
};
}
for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) {
const porops = {} as any;
const errors = {} as any;
if (endpoint.meta.errors) {
for (const e of Object.values(endpoint.meta.errors)) {
errors[e.code] = {
value: {
error: e
}
};
}
}
if (endpoint.meta.params) {
for (const kv of Object.entries(endpoint.meta.params)) {
if (kv[1].desc) (kv[1].validator as any).desc = kv[1].desc[lang];
if (kv[1].default) (kv[1].validator as any).default = kv[1].default;
porops[kv[0]] = kv[1].validator;
}
}
const required = endpoint.meta.params ? Object.entries(endpoint.meta.params).filter(([k, v]) => !v.validator.isOptional).map(([k, v]) => k) : [];
const resSchema = endpoint.meta.res ? renderType(endpoint.meta.res) : {};
function renderType(x: any) {
const res = {} as any;
if (['User', 'Note', 'DriveFile'].includes(x.type)) {
res['$ref'] = `#/components/schemas/${x.type}`;
} else if (x.type === 'object') {
res['type'] = 'object';
if (x.props) {
const props = {} as any;
for (const kv of Object.entries(x.props)) {
props[kv[0]] = renderType(kv[1]);
}
res['properties'] = props;
}
} else if (x.type === 'array') {
res['type'] = 'array';
if (x.items) {
res['items'] = renderType(x.items);
}
} else {
res['type'] = x.type;
}
return res;
}
const info = {
operationId: endpoint.name,
summary: endpoint.name,
description: endpoint.meta.desc ? endpoint.meta.desc[lang] : 'No description provided.',
externalDocs: {
description: 'Source code',
url: `https://github.com/syuilo/misskey/blob/develop/src/server/api/endpoints/${endpoint.name}.ts`
},
...(endpoint.meta.tags ? {
tags: endpoint.meta.tags
} : {}),
...(endpoint.meta.requireCredential ? {
security: [{
ApiKeyAuth: []
}]
} : {}),
requestBody: {
required: true,
content: {
'application/json': {
schema: {
type: 'object',
...(required.length > 0 ? { required } : {}),
properties: endpoint.meta.params ? genProps(porops) : {}
}
}
}
},
responses: {
...(endpoint.meta.res ? {
'200': {
description: 'OK (with results)',
content: {
'application/json': {
schema: resSchema
}
}
}
} : {
'204': {
description: 'OK (without any results)',
}
}),
'400': {
description: 'Client error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
},
examples: { ...errors, ...basicErrors['400'] }
}
}
},
'401': {
description: 'Authentication error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
},
examples: basicErrors['401']
}
}
},
'403': {
description: 'Forbiddon error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
},
examples: basicErrors['403']
}
}
},
'418': {
description: 'I\'m Ai',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
},
examples: basicErrors['418']
}
}
},
...(endpoint.meta.limit ? {
'429': {
description: 'To many requests',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
},
examples: basicErrors['429']
}
}
}
} : {}),
'500': {
description: 'Internal server error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
},
examples: basicErrors['500']
}
}
},
}
};
spec.paths['/' + endpoint.name] = {
post: info
};
}
return spec;
}
|