googleapis / google-api-dotnet-client

Google APIs Client Library for .NET
https://developers.google.com/api-client-library/dotnet
Apache License 2.0
1.34k stars 526 forks source link

Unable to connect to Google Sheets using Quickstart tutorial #1596

Closed alec-seyn closed 4 years ago

alec-seyn commented 4 years ago

Hi, I have been running the .NET Quickstart tutorial for the Google Sheets API succesfully for a couple of weeks as Desktop Application. But the last two days it doesn't seem to connect anymore via OAuth2.

I get an AggregateException on GoogleWebAuthorizationBroker.AuthorizeAsync(...) with the following inner exception.

Any ideas on how to solve this. Thanks!

Google.Apis.Auth.OAuth2.LocalServerCodeReceiver+LimitedLocalhostHttpServer+ServerException:
Unexpected end of network stream reading request line.

   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.LimitedLocalhostHttpServer.ReadRequestLine(Func`1 getChar) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\LocalServerCodeReceiver.cs:line 282
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.LimitedLocalhostHttpServer.GetQueryParamsFromClientAsync(TcpClient client, CancellationToken cancellationToken) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\LocalServerCodeReceiver.cs:line 266
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.LimitedLocalhostHttpServer.GetQueryParamsAsync(CancellationToken cancellationToken) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\LocalServerCodeReceiver.cs:line 207
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.GetResponseFromListener(LimitedLocalhostHttpServer server, CancellationToken ct) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\LocalServerCodeReceiver.cs:line 461
   at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\LocalServerCodeReceiver.cs:line 432
   at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.AuthorizeAsync(String userId, CancellationToken taskCancellationToken) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\AuthorizationCodeInstalledApp.cs:line 78
   at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(Initializer initializer, IEnumerable`1 scopes, String user, CancellationToken taskCancellationToken, IDataStore dataStore, ICodeReceiver codeReceiver) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\GoogleWebAuthorizationBroker.cs:line 154
   at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(ClientSecrets clientSecrets, IEnumerable`1 scopes, String user, CancellationToken taskCancellationToken, IDataStore dataStore, ICodeReceiver codeReceiver) in C:\Apiary\2020-07-01.13-31-04\Src\Support\Google.Apis.Auth\OAuth2\GoogleWebAuthorizationBroker.cs:line 70
jskeet commented 4 years ago

Both @amanda-tarafa and I are on vacation tomorrow, but we'll look on Monday.

jskeet commented 4 years ago

Looking at the stack trace, I suspect it started failing when you upgrade to a new version of one of your dependencies - is that correct? If so, the problem is almost certainly due to one of the changes made in the Auth library recently. Will investigate on that basis. (I strongly suspect that no API is required to reproduce this.)

jskeet commented 4 years ago

Hmm... I can't reproduce the problem with a small sample... please could you try this sample yourself?

using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var secrets = GoogleClientSecrets.Load(File.OpenRead("client_secrets.json")).Secrets;
        string[] scopes = { "https://www.googleapis.com/auth/spreadsheets.readonly" };

        var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            secrets, scopes, "user", CancellationToken.None, dataStore: new NullDataStore());
        Console.WriteLine(credential.UserId);
    }
}

My project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Apis.Auth" Version="1.47.0" />
  </ItemGroup>
</Project>

If you could also let us know more details of your environment (operating system, .NET version etc) that would be really helpful... along with the package versions you're using in the program that's failing for you.

alec-seyn commented 4 years ago

Hi @jskeet, thanks for looking into it.

As far as I remember, I didn't update any package and the problem started occurring. As an attempt to fix, I update to the lastest google sheets api package, but that didn't help.

I should have the lastest version of the google api sheets package, but I am targetting .net core 2.1. (See project file below) Would it help to update to .net core 3?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AssemblyName>OSMagic.Lib</AssemblyName>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AngleSharp" Version="0.14.0" />
    <PackageReference Include="Google.Apis.Sheets.v4" Version="1.47.0.2007" />
    <PackageReference Include="RestSharp" Version="106.11.4" />
  </ItemGroup>

</Project>

I will try the sample you provided and will let you know if that works. thanks!

jskeet commented 4 years ago

Okay, if it changed without you updating the package, that suggests it may be some local change, e.g. in your networking, or perhaps Windows Update.

Did you try the code I provided to reproduce the problem?

alec-seyn commented 4 years ago

Just tried the sample, targetted at .net core 3.1 and google api auth 1.47.0

The first time I ran the code, the program was able to run fully and I got a valid response back.

The next times I ran the code, the issue appears again. After I press 'Allow' on the 'confirm your choices' in the wizard, it takes about a minute for the server to respond and an exception arises.

The exception is the following (occurs on AuthorizeAsync) : Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.LimitedLocalhostHttpServer.ServerException: 'Unexpected end of network stream reading request line.'

My operating system is Windows 10. If I check my updates, I have the following one pending - '2020-01 Update for Windows 10 Version 1903 for x64-based Systems (KB4497165)'.

jskeet commented 4 years ago

Hmmm... not sure what would be going on in that case. Could you open the Network tab in your browser to see the request/response to the auth server, and then the request to the local auth receiver?

alec-seyn commented 4 years ago

This is the request/response to the auth server

Request URL: https://accounts.google.com/signin/oauth/consent/approval?authuser=0&hl=en&_reqid=63945&rt=j
Request Method: POST
Status Code: 200 
Remote Address: [2a00:1450:400e:804::200d]:443
Referrer Policy: no-referrer-when-downgrade

Response Headers
alt-svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-25=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-type: application/json; charset=utf-8
date: Mon, 06 Jul 2020 15:45:45 GMT
expires: Mon, 01 Jan 1990 00:00:00 GMT
pragma: no-cache
server: GSE
set-cookie: GAPS=1:0Me_8YKvrpS_KoIYRSU8qtY96_M2K11-h3DaOt4wUgu-uI5jN1YPjg9T9Yx0eEl8k9l58oObpqkIhysyBbzhRQdZEePc8Q:YzpwfvPj9CK7dTFS;Path=/;Expires=Wed, 06-Jul-2022 15:45:45 GMT;Secure;HttpOnly;Priority=HIGH
set-cookie: __Host-GAPS=1:0Me_8YKvrpS_KoIYRSU8qtY96_M2K11-h3DaOt4wUgu-uI5jN1YPjg9T9Yx0eEl8k9l58oObpqkIhysyBbzhRQdZEePc8Q:YzpwfvPj9CK7dTFS;Path=/;Expires=Wed, 06-Jul-2022 15:45:45 GMT;Secure;HttpOnly;Priority=HIGH
set-cookie: SIDCC=AJi4QfFsZujBRTbhuhqonyzMVUVuTn1CH1pa8ovJ4R2NViEhOfIjNOXBqOaCdVRtce-y93SXt40; expires=Tue, 06-Jul-2021 15:45:45 GMT; path=/; domain=.google.com; priority=high
status: 200
strict-transport-security: max-age=31536000; includeSubDomains
x-content-type-options: nosniff
x-frame-options: DENY
x-xss-protection: 1; mode=bloc

Request Headers
:authority: accounts.google.com
:method: POST
:path: /signin/oauth/consent/approval?authuser=0&hl=en&_reqid=63945&rt=j
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,en-BE;q=0.8,nl-BE;q=0.7,nl;q=0.6,fr;q=0.5,de;q=0.4
content-length: 2839
content-type: application/x-www-form-urlencoded;charset=UTF-8
cookie: CONSENT=YES+DE.en+V8; ACCOUNT_CHOOSER=AFx_qI6OV9U7UJpXpLa-lyh-W5oPCnByDHti8yNAGxrAj5zWEFMP_C3YzTYmzIylqVvexUwgCt3Xq7Sm_DVoqGFXMty2GKUy-KqxwIKHqd4mJf5KTvomYm4YeJbsI1KtA8sdScyO4KRMZ8cE6v3r_tYnCVpxyrZtjFNsvJnI3ukAuJHa5z5CwdgbptHWPuSEYPi9agRULjFBPRmj__8Ca8EXPp9nxzwP_MSsilqaHc8FE8JBKZzczFrmFgxj9m_zNwqWCCZ6Eko-NCN9lMQbRP6bjDVZwP2S0hAe9lJTC_L_IQlBh2cO1C4; HSID=A_SI9iuzhLA3xn2v8; SSID=A5lLJD6DJ1w3T5pvQ; APISID=U2ieyFtdlVbzN-83/AMRbkqWFtPt4V4cct; SAPISID=j6l_JQo0O1qY7DMb/Aezb6cSWttNqZIjKw; __Secure-HSID=A_SI9iuzhLA3xn2v8; __Secure-SSID=A5lLJD6DJ1w3T5pvQ; __Secure-APISID=U2ieyFtdlVbzN-83/AMRbkqWFtPt4V4cct; __Secure-3PAPISID=j6l_JQo0O1qY7DMb/Aezb6cSWttNqZIjKw; SEARCH_SAMESITE=CgQI_48B; SID=ygeZZantjLZGzb0t12HmZzwpEm_NPGj97iZy8XjKB2twdW4B6evMkKo6qQToSLCCR7Rzew.; __Secure-3PSID=ygeZZantjLZGzb0t12HmZzwpEm_NPGj97iZy8XjKB2twdW4BsSGtjiChsVuxsuawWzG_7w.; LSID=doritos|jotspot|lso|o.calendar.google.com|o.console.developers.google.com|o.mail.google.com|o.myaccount.google.com|o.passwords.google.com|o.smartlock.google.com|o.takeout.google.com|s.BE|s.blogger|s.youtube|sitemaps|ss|wise:ygeZZYTBwh2_nHq6lL76qcZXM6lbTdq_H3z5BdPBjsRftNYpNn8CTEpRBw2XJFJzwdyeTQ.; __Host-3PLSID=doritos|jotspot|lso|o.calendar.google.com|o.console.developers.google.com|o.mail.google.com|o.myaccount.google.com|o.passwords.google.com|o.smartlock.google.com|o.takeout.google.com|s.BE|s.blogger|s.youtube|sitemaps|ss|wise:ygeZZYTBwh2_nHq6lL76qcZXM6lbTdq_H3z5BdPBjsRftNYp2mX83uypwswlcKBdhfTaiA.; ANID=AHWqTUndYRI8E39hTiP57uUjy0eeEdlpEmma_s8BSU0XBbwuQmE2cDrIL9-sDg_4; __Secure-3PSIDCC=AJi4QfH4p7e5M_3cgg5cfMKTs5egh8hFCjo6eyfedyLMVs8Pv9OgRqVpi_n847zPk2lQOCzZhg; S=billing-ui-v3=q4pRJnZTONALWRiZKuWVBNYgZRtXO4oe:billing-ui-v3-efe=q4pRJnZTONALWRiZKuWVBNYgZRtXO4oe; user_id=110115131790111108404; 1P_JAR=2020-07-06-13; NID=204=Ap4-TX9U8oKqD4AXFCOyemt81byGoK_inf4VL_Fcvyx8m8-3hr0bESMG7AI4eJVXgt-D3IrMx4lho6wELbx81DhRyzj-_b-TX-2zT0j2M-XLYL32qnxyi00sfaNRwsQb9PNTlLO19ynQtRjFYkQ-IxvZw6lq5rfCPC9BLwIs_E7EVO5gt-iI-WFJzGxoDy0_dNr-H901eO7BbHIazjehIH43P54vi9skYenHfF-LYJ559czvdX8hPluYj03od1PWgY9gJtwxPOinbkphHY01t2YsTyGj9tpCONVS-yRPL4GC80s4OsvcIgBEW0vEDQGBThBFQcGgBbADpR_2xUHowWJ91utHMfE-dcwAgA7ucw; GAPS=1:P9sf78gB_dw1vkY1imiTHjEjsypqbkzP3SZT3h6ziswS-UkDhvNc8Mm-iJ8oBbLOn_bkCDPYDyzP5-TcMYJjs0xerYbaHw:gongvlTRINsvETtY; __Host-GAPS=1:P9sf78gB_dw1vkY1imiTHjEjsypqbkzP3SZT3h6ziswS-UkDhvNc8Mm-iJ8oBbLOn_bkCDPYDyzP5-TcMYJjs0xerYbaHw:gongvlTRINsvETtY; SIDCC=AJi4QfFlbA_usiltvENAkUdgUuOaNwTaljEQ-7vfvIxrKDkI0MibGE9pE2faP8So7quiZEMFTew
google-accounts-xsrf: 1
origin: https://accounts.google.com
referer: https://accounts.google.com/signin/oauth/consentsummary?authuser=0&part=AJi8hAMseiFPZ20kIw11SYspBgYFT-P9tcnisCPWHwZkHfa5SBUhyM4KewslTP3L__pVPLZx57Vkk2ZbmnwLvRTCcBqOI08hWOHnvjHSUVWr47J8ZewIvMA0QCndkyflwqDvTVKqIZkpNgfgEQq6uUh0UaPysHnX_SusuJcMaV3CpBwxQxD0gZMaeutuD6YFGDeAeH7gn4MSjIXgX9a3ZIvX_mhMVZ4E0V5l0QwWm-9xpWHM7dQ1b2O6SINFRDzoI6GX4z41AGicNGu1bpdPgDECMOWxDSJaKmgAvDomva-2m1UcbtwcczEiaYF-O4jGXdGEVBxaOJbVXorRPnyKEaDdTAd966sFbdD7Ok223J1uedajwCHZUGfDlE3pDHzjHqqBAUKn33tmbA99W9RrCJTTHk2J3riMzicNXtHGDjkFv8Aqd-bBcnsUhcmgfxX0vFDaHnLmSjkLmh5HtgWprUaFTY6Bs5DojuOiK61Wt_-IkwkD2291JC72EruhhxrV_1t8KTh0wgk-pgY-yeeg50ztTSwALlz5XEyBy0zwvpAMoz0h7HWmiQfzp6P5VACK9k64ysF2U1tXRYq_hobcdQO8_QNwcZpZFYBY3TY5mIgH6SOydq32xWg&hl=en&as=tmR19XrH9jaKCZsl18novA&rapt=AEjHL4OS0gGkJf_rNn9NaQ1hPpxWJMJeW3CHTZ1xQKR12IljJTqla7r42Fh2tMc2P0_T3g3Fw4GRhuz3bpYyEXgFvUjtcN0DcA&approvedScope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets.readonly
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
x-chrome-connected: mode=0,enable_account_consistency=false,consistency_enabled_by_default=false
x-chrome-id-consistency-request: version=1,client_id=77185425430.apps.googleusercontent.com,device_id=f42e3218-3c59-46db-aeec-a6fb2a4475ba,sync_account_id=110115131790111108404,signin_mode=all_accounts,signout_mode=show_confirmation
x-client-data: CJW2yQEIo7bJAQjEtskBCKmdygEIjsfKAQjnyMoBGJu+ygEYzb7KAQ==
x-same-domain: 1

and this is the request to the localhost. This is where I get an ERR_CONNECTION_REFUSED.

Request URL: http://127.0.0.1:57008/authorize/?code=4/1gGjtLulsBQOCCry_yE1BzpP_Rt1A9f6RRf94RjTbRA2PYyUny6cVVejHzoSceCI5Oy-2DBG8W00B--hiQcRFl4&scope=https://www.googleapis.com/auth/spreadsheets.readonly
Referrer Policy: no-referrer-when-downgrade

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,en-BE;q=0.8,nl-BE;q=0.7,nl;q=0.6,fr;q=0.5,de;q=0.4
Connection: keep-alive
Host: 127.0.0.1:57008
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
jskeet commented 4 years ago

Okay, thanks. I'll compare that with what happens on my machine.

jskeet commented 4 years ago

Hmm. Nothing particularly odd-looking about that request. I don't understand why it would fail.

If you're able to run this in a debugger, it would be useful to break when that exception is thrown, and see what the content of the requestLine local variable is - I wonder whether it's failing to read anything, or whether it's actually read everything but not noticed that it's reached the end of the request.

Adding Amanda as I'm getting to the end of ideas :(

amanda-tarafa commented 4 years ago

I've bee able to reproduce with @jskeet sample code on the first try. I believe this is related to #1055 but that's currently only a hunch. I'm looking into it.

amanda-tarafa commented 4 years ago

We've found the problem and are working on a fix. We'll update this issue with progress.

alec-seyn commented 4 years ago

hi @jskeet & @amanda-tarafa, great! looking forward to the fix

amanda-tarafa commented 4 years ago

I'll update here once the fix has been released.

amanda-tarafa commented 4 years ago

Fix has been released in https://www.nuget.org/packages/Google.Apis.Auth/1.48.0

Thanks for reporting.

alec-seyn commented 4 years ago

thanks for fixing. Works like a charm again :)