377.md 46.1 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
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 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
# Users API

> 原文:[https://docs.gitlab.com/ee/api/users.html](https://docs.gitlab.com/ee/api/users.html)

*   [List users](#list-users)
    *   [For normal users](#for-normal-users)
    *   [For admins](#for-admins)
*   [Single user](#single-user)
    *   [For user](#for-user)
    *   [For admin](#for-admin)
*   [User creation](#user-creation)
*   [User modification](#user-modification)
*   [Delete authentication identity from user](#delete-authentication-identity-from-user)
*   [User deletion](#user-deletion)
*   [List current user (for normal users)](#list-current-user-for-normal-users)
*   [List current user (for admins)](#list-current-user-for-admins)
*   [User status](#user-status)
*   [Get the status of a user](#get-the-status-of-a-user)
*   [Set user status](#set-user-status)
*   [User counts](#user-counts)
*   [List user projects](#list-user-projects)
*   [List SSH keys](#list-ssh-keys)
*   [List SSH keys for user](#list-ssh-keys-for-user)
*   [Single SSH key](#single-ssh-key)
*   [Add SSH key](#add-ssh-key)
*   [Add SSH key for user](#add-ssh-key-for-user)
*   [Delete SSH key for current user](#delete-ssh-key-for-current-user)
*   [Delete SSH key for given user](#delete-ssh-key-for-given-user)
*   [List all GPG keys](#list-all-gpg-keys)
*   [Get a specific GPG key](#get-a-specific-gpg-key)
*   [Add a GPG key](#add-a-gpg-key)
*   [Delete a GPG key](#delete-a-gpg-key)
*   [List all GPG keys for given user](#list-all-gpg-keys-for-given-user)
*   [Get a specific GPG key for a given user](#get-a-specific-gpg-key-for-a-given-user)
*   [Add a GPG key for a given user](#add-a-gpg-key-for-a-given-user)
*   [Delete a GPG key for a given user](#delete-a-gpg-key-for-a-given-user)
*   [List emails](#list-emails)
*   [List emails for user](#list-emails-for-user)
*   [Single email](#single-email)
*   [Add email](#add-email)
*   [Add email for user](#add-email-for-user)
*   [Delete email for current user](#delete-email-for-current-user)
*   [Delete email for given user](#delete-email-for-given-user)
*   [Block user](#block-user)
*   [Unblock user](#unblock-user)
*   [Deactivate user](#deactivate-user)
*   [Activate user](#activate-user)
    *   [Get user contribution events](#get-user-contribution-events)
*   [Get all impersonation tokens of a user](#get-all-impersonation-tokens-of-a-user)
*   [Get an impersonation token of a user](#get-an-impersonation-token-of-a-user)
*   [Create an impersonation token](#create-an-impersonation-token)
*   [Revoke an impersonation token](#revoke-an-impersonation-token)
    *   [Get user activities (admin only)](#get-user-activities-admin-only)
*   [User memberships (admin only)](#user-memberships-admin-only)

# Users API[](#users-api "Permalink")

## List users[](#list-users "Permalink")

活动用户=帐户总数-被阻止的用户

获取用户列表.

此函数使用分页参数`page``per_page`来限制用户列表.

### For normal users[](#for-normal-users "Permalink")

```
GET /users 
```

```
[  {  "id":  1,  "username":  "john_smith",  "name":  "John Smith",  "state":  "active",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/1/cd8.jpeg",  "web_url":  "http://localhost:3000/john_smith"  },  {  "id":  2,  "username":  "jack_smith",  "name":  "Jack Smith",  "state":  "blocked",  "avatar_url":  "http://gravatar.com/../e32131cd8.jpeg",  "web_url":  "http://localhost:3000/jack_smith"  }  ] 
```

您还可以使用`?search=`通过名称或主要电子邮件搜索用户. 例如. `/users?search=John` .

另外,您可以按用户名查找用户:

```
GET /users?username=:username 
```

例如:

```
GET /users?username=jack_smith 
```

另外,您可以根据状态过滤用户,例如. `blocked``active`此选项仅用于过滤被`blocked``active`用户. 它不支持`active=false``blocked=false` .

```
GET /users?active=true 
```

```
GET /users?blocked=true 
```

**注意:**用户名搜索不区分大小写.

### For admins[](#for-admins "Permalink")

```
GET /users 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `order_by` | string | no | 返回按`id``name``username``created_at``updated_at`字段排序的`username` . 默认为`id` |
| `sort` | string | no | 返回用户按`asc``desc`排列. 默认为`desc` |
| `two_factor` | string | no | 通过两因素身份验证过滤用户. `enabled``disabled`过滤器值. 默认情况下,它返回所有用户 |
| `without_projects` | boolean | no | 过滤没有项目的用户. 默认为`false` |

```
[  {  "id":  1,  "username":  "john_smith",  "email":  "john@example.com",  "name":  "John Smith",  "state":  "active",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/1/index.jpg",  "web_url":  "http://localhost:3000/john_smith",  "created_at":  "2012-05-23T08:00:58Z",  "is_admin":  false,  "bio":  null,  "bio_html":  null,  "location":  null,  "skype":  "",  "linkedin":  "",  "twitter":  "",  "website_url":  "",  "organization":  "",  "job_title":  "",  "last_sign_in_at":  "2012-06-01T11:41:01Z",  "confirmed_at":  "2012-05-23T09:05:22Z",  "theme_id":  1,  "last_activity_on":  "2012-05-23",  "color_scheme_id":  2,  "projects_limit":  100,  "current_sign_in_at":  "2012-06-02T06:36:55Z",  "note":  "DMCA Request: 2018-11-05 | DMCA Violation | Abuse | https://gitlab.zendesk.com/agent/tickets/123",  "identities":  [  {"provider":  "github",  "extern_uid":  "2435223452345"},  {"provider":  "bitbucket",  "extern_uid":  "john.smith"},  {"provider":  "google_oauth2",  "extern_uid":  "8776128412476123468721346"}  ],  "can_create_group":  true,  "can_create_project":  true,  "two_factor_enabled":  true,  "external":  false,  "private_profile":  false,  "current_sign_in_ip":  "196.165.1.102",  "last_sign_in_ip":  "172.127.2.22"  },  {  "id":  2,  "username":  "jack_smith",  "email":  "jack@example.com",  "name":  "Jack Smith",  "state":  "blocked",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/2/index.jpg",  "web_url":  "http://localhost:3000/jack_smith",  "created_at":  "2012-05-23T08:01:01Z",  "is_admin":  false,  "bio":  null,  "bio_html":  null,  "location":  null,  "skype":  "",  "linkedin":  "",  "twitter":  "",  "website_url":  "",  "organization":  "",  "job_title":  "",  "last_sign_in_at":  null,  "confirmed_at":  "2012-05-30T16:53:06.148Z",  "theme_id":  1,  "last_activity_on":  "2012-05-23",  "color_scheme_id":  3,  "projects_limit":  100,  "current_sign_in_at":  "2014-03-19T17:54:13Z",  "identities":  [],  "can_create_group":  true,  "can_create_project":  true,  "two_factor_enabled":  true,  "external":  false,  "private_profile":  false,  "current_sign_in_ip":  "10.165.1.102",  "last_sign_in_ip":  "172.127.2.22"  }  ] 
```

使用 GitLab [Starter,Bronze 或更高版本的用户](https://about.gitlab.com/pricing/)还将看到`shared_runners_minutes_limit``extra_shared_runners_minutes_limit`参数.

```
[  {  "id":  1,  ...  "shared_runners_minutes_limit":  133,  "extra_shared_runners_minutes_limit":  133,  ...  }  ] 
```

使用 GitLab [Silver 或更高版本的](https://about.gitlab.com/pricing/)用户还将看到`group_saml`提供程序选项:

```
[  {  "id":  1,  ...  "identities":  [  {"provider":  "github",  "extern_uid":  "2435223452345"},  {"provider":  "bitbucket",  "extern_uid":  "john.smith"},  {"provider":  "google_oauth2",  "extern_uid":  "8776128412476123468721346"},  {"provider":  "group_saml",  "extern_uid":  "123789",  "saml_provider_id":  10}  ],  ...  }  ] 
```

您可以通过外部 UID 和提供程序查找用户:

```
GET /users?extern_uid=:extern_uid&provider=:provider 
```

例如:

```
GET /users?extern_uid=1234567&provider=github 
```

您可以使用`/users?external=true`搜索外部`/users?external=true`

您可以使用以下创建日期时间范围来搜索用户:

```
GET /users?created_before=2001-01-02T00:00:00.060Z&created_after=1999-01-02T00:00:00.060 
```

您可以使用`/users?without_projects=true`搜索没有项目的`/users?without_projects=true`

您可以使用以下[自定义属性](custom_attributes.html)进行过滤:

```
GET /users?custom_attributes[key]=value&custom_attributes[other_key]=other_value 
```

您可以通过以下方式在响应中包括用户的[自定义属性](custom_attributes.html)

```
GET /users?with_custom_attributes=true 
```

## Single user[](#single-user "Permalink")

获得一个用户.

### For user[](#for-user "Permalink")

```
GET /users/:id 
```

Parameters:

*   `id` (必填)-用户的 ID

```
{  "id":  1,  "username":  "john_smith",  "name":  "John Smith",  "state":  "active",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/1/cd8.jpeg",  "web_url":  "http://localhost:3000/john_smith",  "created_at":  "2012-05-23T08:00:58Z",  "bio":  null,  "bio_html":  null,  "location":  null,  "public_email":  "john@example.com",  "skype":  "",  "linkedin":  "",  "twitter":  "",  "website_url":  "",  "organization":  "",  "job_title":  "Operations Specialist"  } 
```

### For admin[](#for-admin "Permalink")

```
GET /users/:id 
```

Parameters:

*   `id` (必填)-用户的 ID

响应示例:

```
{  "id":  1,  "username":  "john_smith",  "email":  "john@example.com",  "name":  "John Smith",  "state":  "active",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/1/index.jpg",  "web_url":  "http://localhost:3000/john_smith",  "created_at":  "2012-05-23T08:00:58Z",  "is_admin":  false,  "bio":  null,  "bio_html":  null,  "location":  null,  "public_email":  "john@example.com",  "skype":  "",  "linkedin":  "",  "twitter":  "",  "website_url":  "",  "organization":  "",  "job_title":  "Operations Specialist",  "last_sign_in_at":  "2012-06-01T11:41:01Z",  "confirmed_at":  "2012-05-23T09:05:22Z",  "theme_id":  1,  "last_activity_on":  "2012-05-23",  "color_scheme_id":  2,  "projects_limit":  100,  "current_sign_in_at":  "2012-06-02T06:36:55Z",  "note":  "DMCA Request: 2018-11-05 | DMCA Violation | Abuse | https://gitlab.zendesk.com/agent/tickets/123",  "identities":  [  {"provider":  "github",  "extern_uid":  "2435223452345"},  {"provider":  "bitbucket",  "extern_uid":  "john.smith"},  {"provider":  "google_oauth2",  "extern_uid":  "8776128412476123468721346"}  ],  "can_create_group":  true,  "can_create_project":  true,  "two_factor_enabled":  true,  "external":  false,  "private_profile":  false,  "current_sign_in_ip":  "196.165.1.102",  "last_sign_in_ip":  "172.127.2.22",  "plan":  "gold",  "trial":  true  } 
```

**注意:** `plan``trial`参数仅在 GitLab 企业版上可用.

使用 GitLab [Starter,Bronze 或更高版本的用户](https://about.gitlab.com/pricing/)还将看到`shared_runners_minutes_limit``extra_shared_runners_minutes_limit`参数.

```
{  "id":  1,  "username":  "john_smith",  "shared_runners_minutes_limit":  133,  "extra_shared_runners_minutes_limit":  133,  ...  } 
```

GitLab.com [Silver 或更高版本](https://about.gitlab.com/pricing/)上的用户还将看到`group_saml`选项:

```
{  "id":  1,  "username":  "john_smith",  "shared_runners_minutes_limit":  133,  "extra_shared_runners_minutes_limit":  133,  "identities":  [  {"provider":  "github",  "extern_uid":  "2435223452345"},  {"provider":  "bitbucket",  "extern_uid":  "john.smith"},  {"provider":  "google_oauth2",  "extern_uid":  "8776128412476123468721346"},  {"provider":  "group_saml",  "extern_uid":  "123789",  "saml_provider_id":  10}  ],  ...  } 
```

您可以通过以下方式在响应中包括用户的[自定义属性](custom_attributes.html)

```
GET /users/:id?with_custom_attributes=true 
```

## User creation[](#user-creation "Permalink")

Creates a new user. Note only administrators can create new users. Either `password`, `reset_password`, or `force_random_password` must be specified. If `reset_password` and `force_random_password` are both `false`, then `password` is required.

请注意, `force_random_password``reset_password`优先级高于`password` . 此外, `reset_password``force_random_password`可以一起使用.

**注意:**[GitLab 12.1 开始](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/29888/)`private_profile`将默认为`false` .

```
POST /users 
```

Parameters:

| Attribute | Required | Description |
| --- | --- | --- |
| `admin` | No | 用户是管理员-正确或错误(默认) |
| `avatar` | No | 用户头像的图像文件 |
| `bio` | No | 用户的传记 |
| `can_create_group` | No | 用户可以创建组-正确或错误 |
| `color_scheme_id` | No | 用户针对文件查看器的配色方案(有关更多信息,请参见[用户首选项文档](../user/profile/preferences.html#syntax-highlighting-theme) ) |
| `email` | Yes | Email |
| `extern_uid` | No | 外部 UID |
| `external` | No | 将用户标记为外部用户-true 或 false(默认) |
| `extra_shared_runners_minutes_limit` | No | 该用户的额外管道分钟配额(在计划中包括的分钟之外购买) |
| `force_random_password` | No | 将用户密码设置为随机值-正确或错误(默认) |
| `group_id_for_saml` | No | 已配置 SAML 的组的 ID |
| `linkedin` | No | LinkedIn |
| `location` | No | 用户位置 |
| `name` | Yes | Name |
| `note` | No | 该用户的管理说明 |
| `organization` | No | 机构名称 |
| `password` | No | Password |
| `private_profile` | No | 用户的个人资料是私有的-true,false(默认)或 null(将转换为 false) |
| `projects_limit` | No | 用户可以创建的项目数 |
| `provider` | No | 外部提供商名称 |
| `public_email` | No | 用户的公开电子邮件 |
| `reset_password` | No | 发送用户密码重置链接-正确或错误(默认) |
| `shared_runners_minutes_limit` | No | 该用户的管道分钟配额(包括在计划中). 可以为`nil` (默认值;继承系统默认值), `0` (无限制)或`> 0` |
| `skip_confirmation` | No | 跳过确认-正确或错误(默认) |
| `skype` | No | Skype 帐号 |
| `theme_id` | No | 用户的 GitLab 主题(有关更多信息,请参见[用户首选项文档](../user/profile/preferences.html#navigation-theme) ) |
| `twitter` | No | Twitter 账号 |
| `username` | Yes | Username |
| `website_url` | No | 网址 |

## User modification[](#user-modification "Permalink")

修改现有用户. 只有管​​理员可以更改用户的属性.

```
PUT /users/:id 
```

Parameters:

| Attribute | Required | Description |
| --- | --- | --- |
| `admin` | No | 用户是管理员-正确或错误(默认) |
| `avatar` | No | 用户头像的图像文件 |
| `bio` | No | 用户的传记 |
| `can_create_group` | No | 用户可以创建组-正确或错误 |
| `color_scheme_id` | No | 用户针对文件查看器的配色方案(有关更多信息,请参见[用户首选项文档](../user/profile/preferences.html#syntax-highlighting-theme) ) |
| `email` | No | Email |
| `extern_uid` | No | 外部 UID |
| `external` | No | 将用户标记为外部用户-true 或 false(默认) |
| `extra_shared_runners_minutes_limit` | No | 该用户的额外管道分钟配额(在计划中包括的分钟之外购买) |
| `group_id_for_saml` | No | 已配置 SAML 的组的 ID |
| `id` | Yes | 用户的 ID |
| `linkedin` | No | LinkedIn |
| `location` | No | 用户位置 |
| `name` | No | Name |
| `note` | No | 该用户的管理说明 |
| `organization` | No | 机构名称 |
| `password` | No | Password |
| `private_profile` | No | 用户的个人资料是私有的-true,false(默认)或 null(将转换为 false) |
| `projects_limit` | No | 限制每个用户可以创建的项目 |
| `provider` | No | 外部提供商名称 |
| `public_email` | No | 用户的公开电子邮件 |
| `shared_runners_minutes_limit` | No | 该用户的管道分钟配额(包括在计划中). 可以为`nil` (默认值;继承系统默认值), `0` (无限制)或`> 0` |
| `skip_reconfirmation` | No | 跳过确认-正确或错误(默认) |
| `skype` | No | Skype 帐号 |
| `theme_id` | No | 用户的 GitLab 主题(有关更多信息,请参见[用户首选项文档](../user/profile/preferences.html#navigation-theme) ) |
| `twitter` | No | Twitter 账号 |
| `username` | No | Username |
| `website_url` | No | 网址 |

密码更新时,用户将被迫在下次登录时进行更改. 请注意,此方法目前仅返回`404`错误,即使在`409` (冲突)更为合适的情况下也是如此. 例如,将电子邮件地址重命名为某个现有地址时.

## Delete authentication identity from user[](#delete-authentication-identity-from-user "Permalink")

使用与该身份关联的提供者名称删除用户的身份验证身份. 仅适用于管理员.

```
DELETE /users/:id/identities/:provider 
```

Parameters:

*   `id` (必填)-用户的 ID
*   `provider` (必填)-外部提供程序名称

## User deletion[](#user-deletion "Permalink")

删除用户. 仅适用于管理员. 如果操作成功,则返回`204 No Content`状态代码;如果找不到资源,则返回`404`如果无法软删除用户,则返回`409` .

```
DELETE /users/:id 
```

Parameters:

*   `id` (必填)-用户的 ID
*   `hard_delete` (可选)-如果为 true,则通常会[移至](../user/profile/account/delete_account.html#associated-records) `hard_delete` [用户的](../user/profile/account/delete_account.html#associated-records)贡献以及该用户`hard_delete`组将被删除.

## List current user (for normal users)[](#list-current-user-for-normal-users "Permalink")

获取当前经过身份验证的用户.

```
GET /user 
```

```
{  "id":  1,  "username":  "john_smith",  "email":  "john@example.com",  "name":  "John Smith",  "state":  "active",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/1/index.jpg",  "web_url":  "http://localhost:3000/john_smith",  "created_at":  "2012-05-23T08:00:58Z",  "bio":  null,  "bio_html":  null,  "location":  null,  "public_email":  "john@example.com",  "skype":  "",  "linkedin":  "",  "twitter":  "",  "website_url":  "",  "organization":  "",  "last_sign_in_at":  "2012-06-01T11:41:01Z",  "confirmed_at":  "2012-05-23T09:05:22Z",  "theme_id":  1,  "last_activity_on":  "2012-05-23",  "color_scheme_id":  2,  "projects_limit":  100,  "current_sign_in_at":  "2012-06-02T06:36:55Z",  "identities":  [  {"provider":  "github",  "extern_uid":  "2435223452345"},  {"provider":  "bitbucket",  "extern_uid":  "john_smith"},  {"provider":  "google_oauth2",  "extern_uid":  "8776128412476123468721346"}  ],  "can_create_group":  true,  "can_create_project":  true,  "two_factor_enabled":  true,  "external":  false,  "private_profile":  false  } 
```

## List current user (for admins)[](#list-current-user-for-admins "Permalink")

Parameters:

*   `sudo` (可选)-在其位置进行呼叫的用户的 ID

```
GET /user 
```

```
{  "id":  1,  "username":  "john_smith",  "email":  "john@example.com",  "name":  "John Smith",  "state":  "active",  "avatar_url":  "http://localhost:3000/uploads/user/avatar/1/index.jpg",  "web_url":  "http://localhost:3000/john_smith",  "created_at":  "2012-05-23T08:00:58Z",  "is_admin":  false,  "bio":  null,  "bio_html":  null,  "location":  null,  "public_email":  "john@example.com",  "skype":  "",  "linkedin":  "",  "twitter":  "",  "website_url":  "",  "organization":  "",  "job_title":  "",  "last_sign_in_at":  "2012-06-01T11:41:01Z",  "confirmed_at":  "2012-05-23T09:05:22Z",  "theme_id":  1,  "last_activity_on":  "2012-05-23",  "color_scheme_id":  2,  "projects_limit":  100,  "current_sign_in_at":  "2012-06-02T06:36:55Z",  "identities":  [  {"provider":  "github",  "extern_uid":  "2435223452345"},  {"provider":  "bitbucket",  "extern_uid":  "john_smith"},  {"provider":  "google_oauth2",  "extern_uid":  "8776128412476123468721346"}  ],  "can_create_group":  true,  "can_create_project":  true,  "two_factor_enabled":  true,  "external":  false,  "private_profile":  false,  "current_sign_in_ip":  "196.165.1.102",  "last_sign_in_ip":  "172.127.2.22"  } 
```

## User status[](#user-status "Permalink")

获取当前登录用户的状态.

```
GET /user/status 
```

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/user/status" 
```

响应示例:

```
{  "emoji":"coffee",  "message":"I crave coffee :coffee:",  "message_html":  "I crave coffee <gl-emoji title=\"hot beverage\" data-name=\"coffee\" data-unicode-version=\"4.0\">☕</gl-emoji>"  } 
```

## Get the status of a user[](#get-the-status-of-a-user "Permalink")

获取用户的状态.

```
GET /users/:id_or_username/status 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id_or_username` | string | yes | 用户的 ID 或用户名,以获取状态 |

```
curl "https://gitlab.example.com/users/janedoe/status" 
```

响应示例:

```
{  "emoji":"coffee",  "message":"I crave coffee :coffee:",  "message_html":  "I crave coffee <gl-emoji title=\"hot beverage\" data-name=\"coffee\" data-unicode-version=\"4.0\">☕</gl-emoji>"  } 
```

## Set user status[](#set-user-status "Permalink")

设置当前用户的状态.

```
PUT /user/status 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `emoji` | string | no | 用作状态的表情符号名称. 如果省略,则使用`speech_balloon` . 表情符号名称可以是[Gemojione 索引中](https://github.com/bonusly/gemojione/blob/master/config/index.json)指定的名称之一. |
| `message` | string | no | 设置为状态的消息. 它还可以包含表情符号代码. |

当参数`emoji``message`都为空时,状态将被清除.

```
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" --data "emoji=coffee" --data "message=I crave coffee" "https://gitlab.example.com/api/v4/user/status" 
```

回应范例

```
{  "emoji":"coffee",  "message":"I crave coffee",  "message_html":  "I crave coffee"  } 
```

## User counts[](#user-counts "Permalink")

获取当前登录用户的计数(与右上角菜单相同).

| Attribute | Type | Description |
| --- | --- | --- |
| `merge_requests` | number | 合并活动的请求并分配给当前用户. |

```
GET /user_counts 
```

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/user_counts" 
```

响应示例:

```
{  "merge_requests":  4  } 
```

## List user projects[](#list-user-projects "Permalink")

请参考[用户项目列表](projects.html#list-user-projects) .

## List SSH keys[](#list-ssh-keys "Permalink")

获取当前经过身份验证的用户的 SSH 密钥的列表.

```
GET /user/keys 
```

```
[  {  "id":  1,  "title":  "Public key",  "key":  "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=",  "created_at":  "2014-08-01T14:47:39.080Z"  },  {  "id":  3,  "title":  "Another Public key",  "key":  "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=",  "created_at":  "2014-08-01T14:47:39.080Z"  }  ] 
```

Parameters:

*   **none**

## List SSH keys for user[](#list-ssh-keys-for-user "Permalink")

获取指定用户的 SSH 密钥的列表.

```
GET /users/:id_or_username/keys 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id_or_username` | string | yes | 要为其获取 SSH 密钥的用户的 ID 或用户名. |

## Single SSH key[](#single-ssh-key "Permalink")

获取一个密钥.

```
GET /user/keys/:key_id 
```

Parameters:

*   `key_id` (必填) `key_id`密钥的 ID

```
{  "id":  1,  "title":  "Public key",  "key":  "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=",  "created_at":  "2014-08-01T14:47:39.080Z"  } 
```

## Add SSH key[](#add-ssh-key "Permalink")

创建当前经过身份验证的用户拥有的新密钥.

```
POST /user/keys 
```

Parameters:

*   `title` (必填)-新的 SSH 密钥的标题
*   `key` (必填)-新的 SSH 密钥
*   `expires_at` (可选)-ISO 8601 格式的 SSH 密钥的到期日期( `YYYY-MM-DDTHH:MM:SSZ`

```
{  "title":  "ABC",  "key":  "ssh-dss AAAAB3NzaC1kc3MAAACBAMLrhYgI3atfrSD6KDas1b/3n6R/HP+bLaHHX6oh+L1vg31mdUqK0Ac/NjZoQunavoyzqdPYhFz9zzOezCrZKjuJDS3NRK9rspvjgM0xYR4d47oNZbdZbwkI4cTv/gcMlquRy0OvpfIvJtjtaJWMwTLtM5VhRusRuUlpH99UUVeXAAAAFQCVyX+92hBEjInEKL0v13c/egDCTQAAAIEAvFdWGq0ccOPbw4f/F8LpZqvWDydAcpXHV3thwb7WkFfppvm4SZte0zds1FJ+Hr8Xzzc5zMHe6J4Nlay/rP4ewmIW7iFKNBEYb/yWa+ceLrs+TfR672TaAgO6o7iSRofEq5YLdwgrwkMmIawa21FrZ2D9SPao/IwvENzk/xcHu7YAAACAQFXQH6HQnxOrw4dqf0NqeKy1tfIPxYYUZhPJfo9O0AmBW2S36pD2l14kS89fvz6Y1g8gN/FwFnRncMzlLY/hX70FSc/3hKBSbH6C6j8hwlgFKfizav21eS358JJz93leOakJZnGb8XlWvz1UJbwCsnR2VEY8Dz90uIk1l/UqHkA= loic@call",  "expires_at":  "2016-01-21T00:00:00.000Z"  } 
```

将返回状态为`201 Created`成功的已创建密钥. 如果发生错误,则会返回`400 Bad Request` ,并附有一条解释该错误的消息:

```
{  "message":  {  "fingerprint":  [  "has already been taken"  ],  "key":  [  "has already been taken"  ]  }  } 
```

## Add SSH key for user[](#add-ssh-key-for-user "Permalink")

创建指定用户拥有的新密钥. 仅适用于管理员

```
POST /users/:id/keys 
```

Parameters:

*   `id` (必填)-指定用户的 ID
*   `title` (必填)-新的 SSH 密钥的标题
*   `key` (必填)-新的 SSH 密钥
*   `expires_at` (可选)-ISO 8601 格式的 SSH 密钥的到期日期( `YYYY-MM-DDTHH:MM:SSZ`

**注意:**这还会添加一个审核事件,如[审核实例事件中所述](../administration/audit_events.html#instance-events-premium-only) .

## Delete SSH key for current user[](#delete-ssh-key-for-current-user "Permalink")

删除当前经过身份验证的用户拥有的密钥. 如果操作成功,则返回`204 No Content`状态代码;如果找不到资源,则返回`404` .

```
DELETE /user/keys/:key_id 
```

Parameters:

*   `key_id` (必填) `key_id`密钥 ID

## Delete SSH key for given user[](#delete-ssh-key-for-given-user "Permalink")

删除指定用户拥有的密钥. 仅适用于管理员.

```
DELETE /users/:id/keys/:key_id 
```

Parameters:

*   `id` (必填)-指定用户的 ID
*   `key_id` (必填) `key_id`密钥 ID

## List all GPG keys[](#list-all-gpg-keys "Permalink")

获取当前已验证用户的 GPG 密钥的列表.

```
GET /user/gpg_keys 
```

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/user/gpg_keys" 
```

响应示例:

```
[  {  "id":  1,  "key":  "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFVjnlIBCACibzXOLCiZiL2oyzYUaTOCkYnSUhymg3pdbfKtd4mpBa58xKBj\r\nt1pTHVpw3Sk03wmzhM/Ndlt1AV2YhLv++83WKr+gAHFYFiCV/tnY8bx3HqvVoy8O\r\nCfxWhw4QZK7+oYzVmJj8ZJm3ZjOC4pzuegNWlNLCUdZDx9OKlHVXLCX1iUbjdYWa\r\nqKV6tdV8hZolkbyjedQgrpvoWyeSHHpwHF7yk4gNJWMMI5rpcssL7i6mMXb/sDzO\r\nVaAtU5wiVducsOa01InRFf7QSTxoAm6Xy0PGv/k48M6xCALa9nY+BzlOv47jUT57\r\nvilf4Szy9dKD0v9S0mQ+IHB+gNukWrnwtXx5ABEBAAHNFm5hbWUgKGNvbW1lbnQp\r\nIDxlbUBpbD7CwHUEEwECACkFAlVjnlIJEINgJNgv009/AhsDAhkBBgsJCAcDAgYV\r\nCAIJCgsEFgIDAQAAxqMIAFBHuBA8P1v8DtHonIK8Lx2qU23t8Mh68HBIkSjk2H7/\r\noO2cDWCw50jZ9D91PXOOyMPvBWV2IE3tARzCvnNGtzEFRtpIEtZ0cuctxeIF1id5\r\ncrfzdMDsmZyRHAOoZ9VtuD6mzj0ybQWMACb7eIHjZDCee3Slh3TVrLy06YRdq2I4\r\nbjMOPePtK5xnIpHGpAXkB3IONxyITpSLKsA4hCeP7gVvm7r7TuQg1ygiUBlWbBYn\r\niE5ROzqZjG1s7dQNZK/riiU2umGqGuwAb2IPvNiyuGR3cIgRE4llXH/rLuUlspAp\r\no4nlxaz65VucmNbN1aMbDXLJVSqR1DuE00vEsL1AItI=\r\n=XQoy\r\n-----END PGP PUBLIC KEY BLOCK-----",  "created_at":  "2017-09-05T09:17:46.264Z"  }  ] 
```

## Get a specific GPG key[](#get-a-specific-gpg-key "Permalink")

获取当前已认证用户的特定 GPG 密钥.

```
GET /user/gpg_keys/:key_id 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `key_id` | integer | yes | GPG 密钥的 ID |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/user/gpg_keys/1" 
```

响应示例:

```
 {  "id":  1,  "key":  "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFVjnlIBCACibzXOLCiZiL2oyzYUaTOCkYnSUhymg3pdbfKtd4mpBa58xKBj\r\nt1pTHVpw3Sk03wmzhM/Ndlt1AV2YhLv++83WKr+gAHFYFiCV/tnY8bx3HqvVoy8O\r\nCfxWhw4QZK7+oYzVmJj8ZJm3ZjOC4pzuegNWlNLCUdZDx9OKlHVXLCX1iUbjdYWa\r\nqKV6tdV8hZolkbyjedQgrpvoWyeSHHpwHF7yk4gNJWMMI5rpcssL7i6mMXb/sDzO\r\nVaAtU5wiVducsOa01InRFf7QSTxoAm6Xy0PGv/k48M6xCALa9nY+BzlOv47jUT57\r\nvilf4Szy9dKD0v9S0mQ+IHB+gNukWrnwtXx5ABEBAAHNFm5hbWUgKGNvbW1lbnQp\r\nIDxlbUBpbD7CwHUEEwECACkFAlVjnlIJEINgJNgv009/AhsDAhkBBgsJCAcDAgYV\r\nCAIJCgsEFgIDAQAAxqMIAFBHuBA8P1v8DtHonIK8Lx2qU23t8Mh68HBIkSjk2H7/\r\noO2cDWCw50jZ9D91PXOOyMPvBWV2IE3tARzCvnNGtzEFRtpIEtZ0cuctxeIF1id5\r\ncrfzdMDsmZyRHAOoZ9VtuD6mzj0ybQWMACb7eIHjZDCee3Slh3TVrLy06YRdq2I4\r\nbjMOPePtK5xnIpHGpAXkB3IONxyITpSLKsA4hCeP7gVvm7r7TuQg1ygiUBlWbBYn\r\niE5ROzqZjG1s7dQNZK/riiU2umGqGuwAb2IPvNiyuGR3cIgRE4llXH/rLuUlspAp\r\no4nlxaz65VucmNbN1aMbDXLJVSqR1DuE00vEsL1AItI=\r\n=XQoy\r\n-----END PGP PUBLIC KEY BLOCK-----",  "created_at":  "2017-09-05T09:17:46.264Z"  } 
```

## Add a GPG key[](#add-a-gpg-key "Permalink")

创建由当前身份验证用户拥有的新 GPG 密钥.

```
POST /user/gpg_keys 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| key | string | yes | 新的 GPG 密钥 |

```
curl --data "key=-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFV..."  --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/user/gpg_keys" 
```

响应示例:

```
[  {  "id":  1,  "key":  "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFVjnlIBCACibzXOLCiZiL2oyzYUaTOCkYnSUhymg3pdbfKtd4mpBa58xKBj\r\nt1pTHVpw3Sk03wmzhM/Ndlt1AV2YhLv++83WKr+gAHFYFiCV/tnY8bx3HqvVoy8O\r\nCfxWhw4QZK7+oYzVmJj8ZJm3ZjOC4pzuegNWlNLCUdZDx9OKlHVXLCX1iUbjdYWa\r\nqKV6tdV8hZolkbyjedQgrpvoWyeSHHpwHF7yk4gNJWMMI5rpcssL7i6mMXb/sDzO\r\nVaAtU5wiVducsOa01InRFf7QSTxoAm6Xy0PGv/k48M6xCALa9nY+BzlOv47jUT57\r\nvilf4Szy9dKD0v9S0mQ+IHB+gNukWrnwtXx5ABEBAAHNFm5hbWUgKGNvbW1lbnQp\r\nIDxlbUBpbD7CwHUEEwECACkFAlVjnlIJEINgJNgv009/AhsDAhkBBgsJCAcDAgYV\r\nCAIJCgsEFgIDAQAAxqMIAFBHuBA8P1v8DtHonIK8Lx2qU23t8Mh68HBIkSjk2H7/\r\noO2cDWCw50jZ9D91PXOOyMPvBWV2IE3tARzCvnNGtzEFRtpIEtZ0cuctxeIF1id5\r\ncrfzdMDsmZyRHAOoZ9VtuD6mzj0ybQWMACb7eIHjZDCee3Slh3TVrLy06YRdq2I4\r\nbjMOPePtK5xnIpHGpAXkB3IONxyITpSLKsA4hCeP7gVvm7r7TuQg1ygiUBlWbBYn\r\niE5ROzqZjG1s7dQNZK/riiU2umGqGuwAb2IPvNiyuGR3cIgRE4llXH/rLuUlspAp\r\no4nlxaz65VucmNbN1aMbDXLJVSqR1DuE00vEsL1AItI=\r\n=XQoy\r\n-----END PGP PUBLIC KEY BLOCK-----",  "created_at":  "2017-09-05T09:17:46.264Z"  }  ] 
```

## Delete a GPG key[](#delete-a-gpg-key "Permalink")

删除当前经过身份验证的用户拥有的 GPG 密钥.

```
DELETE /user/gpg_keys/:key_id 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `key_id` | integer | yes | GPG 密钥的 ID |

```
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/user/gpg_keys/1" 
```

成功时返回`204 No Content` ,如果`404 Not found`密钥,则返回`404 Not found` found.

## List all GPG keys for given user[](#list-all-gpg-keys-for-given-user "Permalink")

获取指定用户的 GPG 密钥的列表. 仅适用于管理员.

```
GET /users/:id/gpg_keys 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 用户的 ID |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/2/gpg_keys" 
```

响应示例:

```
[  {  "id":  1,  "key":  "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFVjnlIBCACibzXOLCiZiL2oyzYUaTOCkYnSUhymg3pdbfKtd4mpBa58xKBj\r\nt1pTHVpw3Sk03wmzhM/Ndlt1AV2YhLv++83WKr+gAHFYFiCV/tnY8bx3HqvVoy8O\r\nCfxWhw4QZK7+oYzVmJj8ZJm3ZjOC4pzuegNWlNLCUdZDx9OKlHVXLCX1iUbjdYWa\r\nqKV6tdV8hZolkbyjedQgrpvoWyeSHHpwHF7yk4gNJWMMI5rpcssL7i6mMXb/sDzO\r\nVaAtU5wiVducsOa01InRFf7QSTxoAm6Xy0PGv/k48M6xCALa9nY+BzlOv47jUT57\r\nvilf4Szy9dKD0v9S0mQ+IHB+gNukWrnwtXx5ABEBAAHNFm5hbWUgKGNvbW1lbnQp\r\nIDxlbUBpbD7CwHUEEwECACkFAlVjnlIJEINgJNgv009/AhsDAhkBBgsJCAcDAgYV\r\nCAIJCgsEFgIDAQAAxqMIAFBHuBA8P1v8DtHonIK8Lx2qU23t8Mh68HBIkSjk2H7/\r\noO2cDWCw50jZ9D91PXOOyMPvBWV2IE3tARzCvnNGtzEFRtpIEtZ0cuctxeIF1id5\r\ncrfzdMDsmZyRHAOoZ9VtuD6mzj0ybQWMACb7eIHjZDCee3Slh3TVrLy06YRdq2I4\r\nbjMOPePtK5xnIpHGpAXkB3IONxyITpSLKsA4hCeP7gVvm7r7TuQg1ygiUBlWbBYn\r\niE5ROzqZjG1s7dQNZK/riiU2umGqGuwAb2IPvNiyuGR3cIgRE4llXH/rLuUlspAp\r\no4nlxaz65VucmNbN1aMbDXLJVSqR1DuE00vEsL1AItI=\r\n=XQoy\r\n-----END PGP PUBLIC KEY BLOCK-----",  "created_at":  "2017-09-05T09:17:46.264Z"  }  ] 
```

## Get a specific GPG key for a given user[](#get-a-specific-gpg-key-for-a-given-user "Permalink")

获取给定用户的特定 GPG 密钥. 仅适用于管理员.

```
GET /users/:id/gpg_keys/:key_id 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 用户的 ID |
| `key_id` | integer | yes | GPG 密钥的 ID |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/2/gpg_keys/1" 
```

响应示例:

```
 {  "id":  1,  "key":  "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFVjnlIBCACibzXOLCiZiL2oyzYUaTOCkYnSUhymg3pdbfKtd4mpBa58xKBj\r\nt1pTHVpw3Sk03wmzhM/Ndlt1AV2YhLv++83WKr+gAHFYFiCV/tnY8bx3HqvVoy8O\r\nCfxWhw4QZK7+oYzVmJj8ZJm3ZjOC4pzuegNWlNLCUdZDx9OKlHVXLCX1iUbjdYWa\r\nqKV6tdV8hZolkbyjedQgrpvoWyeSHHpwHF7yk4gNJWMMI5rpcssL7i6mMXb/sDzO\r\nVaAtU5wiVducsOa01InRFf7QSTxoAm6Xy0PGv/k48M6xCALa9nY+BzlOv47jUT57\r\nvilf4Szy9dKD0v9S0mQ+IHB+gNukWrnwtXx5ABEBAAHNFm5hbWUgKGNvbW1lbnQp\r\nIDxlbUBpbD7CwHUEEwECACkFAlVjnlIJEINgJNgv009/AhsDAhkBBgsJCAcDAgYV\r\nCAIJCgsEFgIDAQAAxqMIAFBHuBA8P1v8DtHonIK8Lx2qU23t8Mh68HBIkSjk2H7/\r\noO2cDWCw50jZ9D91PXOOyMPvBWV2IE3tARzCvnNGtzEFRtpIEtZ0cuctxeIF1id5\r\ncrfzdMDsmZyRHAOoZ9VtuD6mzj0ybQWMACb7eIHjZDCee3Slh3TVrLy06YRdq2I4\r\nbjMOPePtK5xnIpHGpAXkB3IONxyITpSLKsA4hCeP7gVvm7r7TuQg1ygiUBlWbBYn\r\niE5ROzqZjG1s7dQNZK/riiU2umGqGuwAb2IPvNiyuGR3cIgRE4llXH/rLuUlspAp\r\no4nlxaz65VucmNbN1aMbDXLJVSqR1DuE00vEsL1AItI=\r\n=XQoy\r\n-----END PGP PUBLIC KEY BLOCK-----",  "created_at":  "2017-09-05T09:17:46.264Z"  } 
```

## Add a GPG key for a given user[](#add-a-gpg-key-for-a-given-user "Permalink")

创建指定用户拥有的新 GPG 密钥. 仅适用于管理员.

```
POST /users/:id/gpg_keys 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 用户的 ID |
| `key_id` | integer | yes | GPG 密钥的 ID |

```
curl --data "key=-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFV..."  --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/2/gpg_keys" 
```

响应示例:

```
[  {  "id":  1,  "key":  "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\nxsBNBFVjnlIBCACibzXOLCiZiL2oyzYUaTOCkYnSUhymg3pdbfKtd4mpBa58xKBj\r\nt1pTHVpw3Sk03wmzhM/Ndlt1AV2YhLv++83WKr+gAHFYFiCV/tnY8bx3HqvVoy8O\r\nCfxWhw4QZK7+oYzVmJj8ZJm3ZjOC4pzuegNWlNLCUdZDx9OKlHVXLCX1iUbjdYWa\r\nqKV6tdV8hZolkbyjedQgrpvoWyeSHHpwHF7yk4gNJWMMI5rpcssL7i6mMXb/sDzO\r\nVaAtU5wiVducsOa01InRFf7QSTxoAm6Xy0PGv/k48M6xCALa9nY+BzlOv47jUT57\r\nvilf4Szy9dKD0v9S0mQ+IHB+gNukWrnwtXx5ABEBAAHNFm5hbWUgKGNvbW1lbnQp\r\nIDxlbUBpbD7CwHUEEwECACkFAlVjnlIJEINgJNgv009/AhsDAhkBBgsJCAcDAgYV\r\nCAIJCgsEFgIDAQAAxqMIAFBHuBA8P1v8DtHonIK8Lx2qU23t8Mh68HBIkSjk2H7/\r\noO2cDWCw50jZ9D91PXOOyMPvBWV2IE3tARzCvnNGtzEFRtpIEtZ0cuctxeIF1id5\r\ncrfzdMDsmZyRHAOoZ9VtuD6mzj0ybQWMACb7eIHjZDCee3Slh3TVrLy06YRdq2I4\r\nbjMOPePtK5xnIpHGpAXkB3IONxyITpSLKsA4hCeP7gVvm7r7TuQg1ygiUBlWbBYn\r\niE5ROzqZjG1s7dQNZK/riiU2umGqGuwAb2IPvNiyuGR3cIgRE4llXH/rLuUlspAp\r\no4nlxaz65VucmNbN1aMbDXLJVSqR1DuE00vEsL1AItI=\r\n=XQoy\r\n-----END PGP PUBLIC KEY BLOCK-----",  "created_at":  "2017-09-05T09:17:46.264Z"  }  ] 
```

## Delete a GPG key for a given user[](#delete-a-gpg-key-for-a-given-user "Permalink")

删除指定用户拥有的 GPG 密钥. 仅适用于管理员.

```
DELETE /users/:id/gpg_keys/:key_id 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 用户的 ID |
| `key_id` | integer | yes | The ID of the GPG key |

```
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/2/gpg_keys/1" 
```

## List emails[](#list-emails "Permalink")

获取当前经过身份验证的用户电子邮件列表.

```
GET /user/emails 
```

```
[  {  "id":  1,  "email":  "email@example.com"  },  {  "id":  3,  "email":  "email2@example.com"  }  ] 
```

Parameters:

*   **none**

## List emails for user[](#list-emails-for-user "Permalink")

获取指定用户的电子邮件列表. 仅适用于管理员

```
GET /users/:id/emails 
```

Parameters:

*   `id` (必填)-指定用户的 ID

## Single email[](#single-email "Permalink")

收到一封电子邮件.

```
GET /user/emails/:email_id 
```

Parameters:

*   `email_id` (必填)-电子邮件 ID

```
{  "id":  1,  "email":  "email@example.com"  } 
```

## Add email[](#add-email "Permalink")

创建由当前身份验证用户拥有的新电子邮件.

```
POST /user/emails 
```

Parameters:

*   `email` (必填)-电子邮件地址

```
{  "id":  4,  "email":  "email@example.com"  } 
```

将返回状态为`201 Created`成功的已创建电子邮件. 如果发生错误,则会返回`400 Bad Request` ,并附有一条解释该错误的消息:

```
{  "message":  {  "email":  [  "has already been taken"  ]  }  } 
```

## Add email for user[](#add-email-for-user "Permalink")

创建指定用户拥有的新电子邮件. 仅适用于管理员

```
POST /users/:id/emails 
```

Parameters:

*   `id` (必填)-指定用户的 ID
*   `email` (必填)-电子邮件地址
*   `skip_confirmation` (可选)-跳过确认并假定电子邮件已通过验证`skip_confirmation`或 false(默认)

## Delete email for current user[](#delete-email-for-current-user "Permalink")

删除当前经过身份验证的用户拥有的电子邮件. 如果操作成功,则返回`204 No Content`状态代码;如果找不到资源,则返回`404` .

```
DELETE /user/emails/:email_id 
```

Parameters:

*   `email_id` (必填)-电子邮件 ID

## Delete email for given user[](#delete-email-for-given-user "Permalink")

删除指定用户拥有的电子邮件. 仅适用于管理员.

```
DELETE /users/:id/emails/:email_id 
```

Parameters:

*   `id` (必填)-指定用户的 ID
*   `email_id` (必填)-电子邮件 ID

## Block user[](#block-user "Permalink")

阻止指定的用户. 仅适用于管理员.

```
POST /users/:id/block 
```

Parameters:

*   `id` (必填)-指定用户的 ID

Returns:

*   `201 OK`成功成功.
*   `404 User Not Found`如果`404 User Not Found`用户,则`404 User Not Found`用户.
*   `403 Forbidden`尝试通过 LDAP 同步阻止已阻止的用户时禁止.

## Unblock user[](#unblock-user "Permalink")

取消阻止指定的用户. 仅适用于管理员.

```
POST /users/:id/unblock 
```

Parameters:

*   `id` (必填)-指定用户的 ID

尝试取消阻止被 LDAP 同步阻止的用户时,成功返回`201 OK` ,成功返回`404 User Not Found``403 Forbidden` .

## Deactivate user[](#deactivate-user "Permalink")

在 GitLab 12.4 中[引入](https://gitlab.com/gitlab-org/gitlab/-/issues/22257) .

停用指定的用户. 仅适用于管理员.

```
POST /users/:id/deactivate 
```

Parameters:

*   `id` (必填)-指定用户的 ID

Returns:

*   `201 OK`成功成功.
*   `404 User Not Found`如果`404 User Not Found`用户,则`404 User Not Found`用户.
*   `403 Forbidden`尝试停用用户时被禁止:
    *   被管理员或 LDAP 同步阻止.
    *   在过去 180 天内没有任何活动. 这些用户无法停用.

## Activate user[](#activate-user "Permalink")

在 GitLab 12.4 中[引入](https://gitlab.com/gitlab-org/gitlab/-/issues/22257) .

激活指定的用户. 仅适用于管理员.

```
POST /users/:id/activate 
```

Parameters:

*   `id` (必填)-指定用户的 ID

Returns:

*   `201 OK`成功成功.
*   `404 User Not Found`如果`404 User Not Found`用户,则`404 User Not Found`用户.
*   `403 Forbidden`尝试激活被管理员或 LDAP 同步阻止的用户时禁止.

### Get user contribution events[](#get-user-contribution-events "Permalink")

请参阅[事件 API 文档](events.html#get-user-contribution-events)

## Get all impersonation tokens of a user[](#get-all-impersonation-tokens-of-a-user "Permalink")

> 需要管理员权限.

它检索用户的每个模拟令牌. 使用分页参数`page``per_page`可以限制模拟令牌的列表.

```
GET /users/:user_id/impersonation_tokens 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `user_id` | integer | yes | 用户的 ID |
| `state` | string | no | 根据状态( `all``active``inactive` )过滤令牌 |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/42/impersonation_tokens" 
```

响应示例:

```
[  {  "active"  :  true,  "scopes"  :  [  "api"  ],  "revoked"  :  false,  "name"  :  "mytoken",  "id"  :  2,  "created_at"  :  "2017-03-17T17:18:09.283Z",  "impersonation"  :  true,  "expires_at"  :  "2017-04-04"  },  {  "active"  :  false,  "scopes"  :  [  "read_user"  ],  "revoked"  :  true,  "name"  :  "mytoken2",  "created_at"  :  "2017-03-17T17:19:28.697Z",  "id"  :  3,  "impersonation"  :  true,  "expires_at"  :  "2017-04-14"  }  ] 
```

## Get an impersonation token of a user[](#get-an-impersonation-token-of-a-user "Permalink")

> 需要管理员权限.

它显示了用户的模拟令牌.

```
GET /users/:user_id/impersonation_tokens/:impersonation_token_id 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `user_id` | integer | yes | 用户的 ID |
| `impersonation_token_id` | integer | yes | 模拟令牌的 ID |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/42/impersonation_tokens/2" 
```

响应示例:

```
{  "active"  :  true,  "scopes"  :  [  "api"  ],  "revoked"  :  false,  "name"  :  "mytoken",  "id"  :  2,  "created_at"  :  "2017-03-17T17:18:09.283Z",  "impersonation"  :  true,  "expires_at"  :  "2017-04-04"  } 
```

## Create an impersonation token[](#create-an-impersonation-token "Permalink")

> 需要管理员权限. 令牌值返回一次. 确保将其保存-您将无法再次访问它.

它创建一个新的模拟令牌. 请注意,只有管理员可以执行此操作. 您只能创建模拟令牌来模拟用户,并执行 API 调用和 Git 读写. 用户将不会在其个人资料设置页面中看到这些令牌.

```
POST /users/:user_id/impersonation_tokens 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `user_id` | integer | yes | 用户的 ID |
| `name` | string | yes | 模拟令牌的名称 |
| `expires_at` | date | no | ISO 格式( `YYYY-MM-DD` )的模拟令牌的到期日期 |
| `scopes` | array | yes | 模拟令牌作用域的数组( `api``read_user` ) |

```
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --data "name=mytoken" --data "expires_at=2017-04-04" --data "scopes[]=api" "https://gitlab.example.com/api/v4/users/42/impersonation_tokens" 
```

响应示例:

```
{  "id"  :  2,  "revoked"  :  false,  "scopes"  :  [  "api"  ],  "token"  :  "EsMo-vhKfXGwX9RKrwiy",  "active"  :  true,  "impersonation"  :  true,  "name"  :  "mytoken",  "created_at"  :  "2017-03-17T17:18:09.283Z",  "expires_at"  :  "2017-04-04"  } 
```

## Revoke an impersonation token[](#revoke-an-impersonation-token "Permalink")

> 需要管理员权限.

它撤销模拟令牌.

```
DELETE /users/:user_id/impersonation_tokens/:impersonation_token_id 
```

```
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/42/impersonation_tokens/1" 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `user_id` | integer | yes | 用户的 ID |
| `impersonation_token_id` | integer | yes | 模拟令牌的 ID |

### Get user activities (admin only)[](#get-user-activities-admin-only "Permalink")

**注意:**此 API 端点仅在 8.15(EE)和 9.1(CE)及更高版本上可用.

获取所有用户的上次活动日期,从最旧到最新.

更新时间戳记的活动是:

*   Git HTTP / SSH 活动(例如克隆,推送)
*   用户登录 GitLab
*   与仪表板,项目,问题和合并请求相关的用户访问页面(在 GitLab 11.8 中[引入](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/54947)
*   用户使用 API
*   用户使用 GraphQL API

默认情况下,它显示最近 6 个月内所有用户的活动,但是可以使用`from`参数进行修改.

```
GET /user/activities 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `from` | string | no | 日期字符串,格式为 YEAR-MONTH-DAY. 例如, `2016-03-11` . 默认为 6 个月前. |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/user/activities" 
```

响应示例:

```
[  {  "username":  "user1",  "last_activity_on":  "2015-12-14",  "last_activity_at":  "2015-12-14"  },  {  "username":  "user2",  "last_activity_on":  "2015-12-15",  "last_activity_at":  "2015-12-15"  },  {  "username":  "user3",  "last_activity_on":  "2015-12-16",  "last_activity_at":  "2015-12-16"  }  ] 
```

请注意, `last_activity_at`已过时,请使用`last_activity_on` .

## User memberships (admin only)[](#user-memberships-admin-only "Permalink")

在 GitLab 12.8 中[引入](https://gitlab.com/gitlab-org/gitlab/-/issues/20532) .

列出用户所属的所有项目和组. 该端点仅适用于管理员. 它返回成员资格的`source_id``source_name``source_type``access_level` . 源可以是`Namespace` (代表一个组)或`Project` . 响应仅代表直接成员身份. 不包括继承的成员资格,例如在子组中. 访问级别由整数值表示. 有关更多详细信息,请阅读有关[访问级别值](access_requests.html#valid-access-levels)的含义.

```
GET /users/:id/memberships 
```

Parameters:

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 指定用户的 ID |
| `type` | string | no | 按类型过滤成员资格. 可以是`Project``Namespace` |

Returns:

*   `200 OK`成功.
*   `404 User Not Found` if user can’t be found.
*   `403 Forbidden`管理员未要求时禁止.
*   不支持请求的类型时出现`400 Bad Request` .

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/:user_id/memberships" 
```

响应示例:

```
[  {  "source_id":  1,  "source_name":  "Project one",  "source_type":  "Project",  "access_level":  "20"  },  {  "source_id":  3,  "source_name":  "Group three",  "source_type":  "Namespace",  "access_level":  "20"  },  ] 
```