gpt4 book ai didi

c# - 使用 Token 获取信息。 OAuth

转载 作者:行者123 更新时间:2023-11-30 13:04:58 26 4
gpt4 key购买 nike

我正在创建一个应用程序以使用 OAuth 从 Fitbit.com 获取信息。

protected void btnConnect_Click(object sender, EventArgs e)
{
// Create OAuthService object, containing oauth consumer configuration
OAuthService service = OAuthService.Create(
new EndPoint(RequestTokenUrl, "POST"), // requestTokenEndPoint
new Uri(AuthorizationUrl), // authorizationUri
new EndPoint(AccessTokenUrl, "POST"), // accessTokenEndPoint
true, // useAuthorizationHeader
"http://app.fitbit.com", // realm
"HMAC-SHA1", // signatureMethod
"1.0", // oauthVersion
new OAuthConsumer(ConsumerKey, ConsumerSecret) // consumer
);

try
{
var personRepository = new PersonRepository();

var person = personRepository.GetPersonById(int.Parse(personSelect.SelectedItem.Value));



OAuthRequest request = OAuthRequest.Create(
new EndPoint(ProfileUrl, "GET"),
service,
this.Context.Request.Url,
//this.Context.Session.SessionID);
person.FitbitAuthAccessToken,
);

request.VerificationHandler = AspNetOAuthRequest.HandleVerification;

OAuthResponse response = request.GetResource();

// Check if OAuthResponse object has protected resource
if (!response.HasProtectedResource)
{
var token = new OAuthToken(TokenType.Request, person.FitbitAuthAccessToken,
person.FitbitAuthSecret, ConsumerKey);
// If not we are not authorized yet, build authorization URL and redirect to it
string authorizationUrl = service.BuildAuthorizationUrl(response.Token).AbsoluteUri;
Response.Redirect(authorizationUrl);
}


person.FitbitAuthAccessToken = response.Token.Token;
person.FitbitAuthSecret = response.Token.Secret;
person.PersonEncodedId = Doc["result"]["user"]["encodedId"].InnerText;
personRepository.Update(person);

// Store the access token in session variable
Session["access_token"] = response.Token;
}
catch (WebException ex)
{
Response.Write(ex.Message);
Response.Close();
}
catch (OAuthRequestException ex)
{
Response.Write(ex.Message);
Response.Close();
}
}

我将 Fitbit 访问 token 和密码保存在数据库中。
如何仅使用访问 token 和密码获取信息,而无需每次都授权?

最佳答案

这假设 FitBit api 足够强大,不会每次都需要身份验证。我已经看到 API 在您有身份验证过程的地方实现 OAuth,然后从那里您的大部分调用只需要 AccessToken 或 secret 。我会查看服务的方法签名,看看它们需要什么类型的参数。

关于c# - 使用 Token 获取信息。 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6712994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com