Getting 401 and 403 when running dotnet restore

I am trying to use the basic .NET Core Github workflow as shown here, but I am having trouble with nuget. When the workflow gets to the Install Depedencies which performs a donet restore, it keeps getting 403s and 401 errors when running on my WebAPI repo here. I have tried using, {{secrets.GITHUB\_TOKEN}}, {{secrets.PERSONAL_ACCESS_TOKEN}} with repo and read package permissions and using the actual token instead of the secret reference.

How do I fix this?

@radulfr96 ,

In the " nuget.config" file, I see you set the Credentials like as below:

<packageSourceCredentials>
  <github>
    <add key="Username" value="radulfr96" />
    <add key="ClearTextPassword" value="${{secrets.GITHUB_TOKEN}}" />
  </github>
</packageSourceCredentials>

You should not directly call the secrets in the " nuget.config" file, you need to set the secrets as environment variables at first, then call the environment variables in the " nuget.config" file.

## in the workflow file
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
    steps:
    . . .

## in the "nuget.config" file
<add key="ClearTextPassword" value="${GITHUB_TOKEN}" />
OR
<add key="ClearTextPassword" value="${PERSONAL_ACCESS_TOKEN}" />

Typically, you can directly use the secrets in the value of the with key.

For example:

- name: Checkout code
  uses: actions/checkout@v2
  with:
    token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

Other situations, such as in the command lines or in the config/settings files, you’d better set the secrets as environment variables at first.

More details, you can reference the docs about Secrets.

1 Like

@radulfr96 ,

How are things going? Is my suggestion helpful to you?

Please try it, and any progress, feel free to tell me.

@brightran 

I haven’t had the chance to try it yet. This is a hobby app I work on, on the weekends. I’ll update you then. But your solution looks promising. Thanks

@radulfr96 ,

Okay, any update, feel free to tell me.

@brightran 

I made the changes you reccomended and I am still getting 401s. Do you have any other sugesstions?

@radulfr96 ,

How about " <add key=“ClearTextPassword” value="%GITHUB_TOKEN%" />"?

I tested, it can work fine, see the example here:

https://github.com/TestGitHubPackagesRML/TestNuGet/blob/master/TestNuGet/nuget.config

https://github.com/TestGitHubPackagesRML/TestNuGet/blob/master/TestNuGet/TestNuGet.csproj

https://github.com/TestGitHubPackagesRML/TestNuGet/blob/master/.github/workflows/CI.yml

1 Like

@brightran,

Hi 

I changed it to that and it appears to work but now I am getting 

error NU1101: Unable to find package Microsoft.EntityFrameworkCore. No packages exist with this id in source(s): github

Which I assume is because my nuget.config does not reference nuget.org

Edit: I fixed it and it is now building, thank you.