sách gpt4 ai đã đi

.net - 如何通过.NET中的Google Drive SDK使用刷新 token 生成访问 token ?

In lại 作者:行者123 更新时间:2023-12-02 15:10:42 26 4
mua khóa gpt4 Nike

我有一个 .NET 应用程序,它使用 Google 云端硬盘来访问用户的文件。我能够获取授权码,并且我已经能够通过AccessToken和RefreshToken交换授权码。问题是我无法刷新访问 token ,并且它会在一小时后过期。

类似这个问题:How to generate access token using refresh token through google drive API?除了我在 .NET 中工作(使用 Google.API...DLL)。

我知道这一点:https://developers.google.com/accounts/docs/OAuth2InstalledApp#refresh但是,我期望 IAuthorizationState 或 OAuth2Authenticator 对象中提供某种方法来允许我刷新访问 token 。

请指教。谢谢。

请注意,使用此代码我可以获得访问 token 。只是我希望这段代码位于 Google API 内部。

    public class OAuth2AccessTokenReponse
{
public string access_token;
public int expires_in;
public string token_type;
}
public static string refreshAccessToken()
{
using (System.Net.WebClient client = new System.Net.WebClient())
{
byte[] response = client.UploadValues("https://accounts.google.com/o/oauth2/token", new System.Collections.Specialized.NameValueCollection(){
{"client_id", ClientID},
{"client_secret", ClientSecret},
{"refresh_token", "XXXXX"},
{"grant_type", "refresh_token"}
});
string sresponse = System.Text.Encoding.Default.GetString(response);
OAuth2AccessTokenReponse o = (OAuth2AccessTokenReponse) Newtonsoft.Json.JsonConvert.DeserializeObject(sresponse, typeof(OAuth2AccessTokenReponse));
return o.access_token;
}
}

1 Câu trả lời

我研究了一个更合适的示例:GoogleApisSample 的 Tasks.WinForms.NoteMgr...并用它找到了解决方案。

解决方案在下面的代码中。它的关键部分是调用arg.RefreshToken(state);

Cảm ơn.

    public static Authentication.IAuthenticator UseSavedAuthorization()
{

var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = ClientID;
provider.ClientSecret = ClientSecret;

OAuth2Authenticator auth = new OAuth2Authenticator(provider, getState);

auth.LoadAccessToken();

return auth;
}


public static IAuthorizationState getState(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue(),
DriveService.Scopes.DriveFile.GetStringValue() , DriveService.Scopes.Drive.GetStringValue()
});
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);

state.RefreshToken = "";
arg.RefreshToken(state);

return state;
}`

关于.net - 如何通过.NET中的Google Drive SDK使用刷新 token 生成访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11458644/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com