0

I am retriving a token through an website, then I save the token, and refresh token in a database. Then I am writing .net program that uploads video's located on our server to youtube. My problem is to get the program I am making, use the stored token. I am using one of google's examples to upload the video. But the program should use the already saved token, instead os asking for new credentials.

The current code is this.

UserCredential credential;

        //credential.UserCredential(,"",)

        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows an application to upload files to the
                // authenticated user's YouTube channel, but doesn't allow other types of access.
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None
            );

        }
        Console.WriteLine("HER");

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

So instead I would like to use an already saved token.

Any help, or a pointer in the right direction would be great. Thanks.

esso74
  • 1
  • 1
  • How old are the tokens that you're storing? A normal access token expires after an hour or so, and so you can't use it repeatedly without getting a new one. When you first store the access token and the refresh token, also store the expiry time of the access token. Then, before ever making a call, check your time to see if it's expired. If it is, use the refresh token to get a new access token and set it, and you should be good to go. – jlmcdonald Apr 12 '14 at 20:53
  • Hi, Thanks. I am storing the refreshtoken also, so I can refresh it. But my problem is how to use the stored(valid) token to upload a video. The above code asks for a new credentials. So I would like to change the above code, to use the stored access token. – esso74 Apr 13 '14 at 13:02

1 Answers1

2

You would use GoogleAuthorizationCodeFlow instead of GoogleWebAuthorizationBroker in order to use the refresh token. See the answer here.

Community
  • 1
  • 1
Haider
  • 1,488
  • 2
  • 15
  • 29