Using basic authentication

Basic authentication with a username and password is the simplest method available:

$params = @{
Uri = 'https://api.github.com/user/emails'
Credential = Get-Credential
}
Invoke-RestMethod @params

In PowerShell Core, the Authentication parameter should be added:

$params = @{
Uri = 'https://api.github.com/user/emails'
Credential = Get-Credential
Authentication = 'Basic'
}
Invoke-RestMethod @params

If the account is configured to use two-factor authentication, this request may fail with the following error message:

PS> Invoke-RestMethod @params
Invoke-RestMethod : {"message":"Must specify two-factor authentication OTP code.","documentation_url":"https://developer.github.com/v3/auth#working-with-two-factor-authentication"}

At line:1 char:1
+ Invoke-RestMethod https://api.github.com/user/emails -Credential $cre ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: GET, Re...rShell/6.1.0
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

GitHub provides documentation showing how to add the second authentication factor, although it is not clear how SMS tokens can be requested: https://developer.github.com/v3/auth/.

In this case, it may be more appropriate to use a personal access token. Personal access tokens can be generated by visiting account settings, then developer settings. Once generated, the personal access token cannot be viewed. The personal access token is used in place of a password.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.151.231