LinkedIn API OAuth 刷新令牌

问题描述 投票:0回答:4

我正在使用 LinkedIn API 从那里提取更新并显示在网站上。使用 OAuth 时,我将令牌存储在文件中,然后再次从那里提取它以防止登录弹出窗口。但是,我不清楚一旦我的令牌过期,它将如何刷新。以下是我如何从文件中读取令牌 -

        $config = json_decode(file_get_contents(".service.dat"));
        if( isset($config->key) && isset($config->secret) ) {
            $this->access_token = new OAuthConsumer($config->key, $config->secret);
        } 

为了进行身份验证,我有以下方法来获取请求令牌 -

function getRequestToken()
{
    $consumer = $this->consumer;
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $this->request_token_path);
    $request->set_parameter("oauth_callback", $this->oauth_callback);
    $request->sign_request($this->signature_method, $consumer, NULL);
    $headers = Array();
    $url = $request->to_url();
    $response = $this->httpRequest($url, $headers, "GET");
    parse_str($response, $response_params);
    $this->request_token = new OAuthConsumer($response_params['oauth_token'], $response_params['oauth_token_secret'], 1);
}

生成令牌后,我正在生成授权网址:

function generateAuthorizeUrl()
{
    $consumer = $this->consumer;
    $request_token = $this->request_token;
    return $this->authorize_path . "?oauth_token=" . $request_token->key;
}

LinkedIn 文档对刷新令牌进行了以下说明:

刷新访问令牌非常简单,无需任何操作即可完成 为用户显示授权对话框。换句话说,这是一个 无缝流程,不会影响应用程序的用户 经验。只需让您的申请通过授权 流以获取具有额外 60 天的新访问令牌 寿命。

我不清楚这意味着什么。如果我必须再次从获取请求令牌开始重做,那么这是否需要我再次发出http请求并必须弹出登录屏幕?我该如何避免呢?非常感谢建议。

谢谢。

php oauth linkedin-api
4个回答
2
投票

发现了。授权网址:

https://www.linkedin.com/oauth/v2/authorization

后跟访问令牌 url:

https://www.linkedin.com/oauth/v2/accessToken

这就是我真正要做的一切(传递正确的参数)。


0
投票

还有一个端点可以在令牌过期后刷新令牌,以下是执行此操作的方法的文档:https://learn.microsoft.com/en-us/linkedin/shared/authentication/programmatic-refresh-代币


0
投票

如果您查看文档

Linkedin 不提供刷新令牌,您需要再次完成工作流程。

这是简短的解释:

要刷新访问令牌,只需再次执行本文档中概述的授权流程即可获取新令牌。 在刷新工作流程期间,如果满足以下条件,流程的授权对话框部分将自动跳过,并将用户重定向回您的回调 URL,从而使获取刷新的访问令牌成为无缝的幕后用户体验

刷新您的访问令牌


0
投票

有一个简单的示例如何刷新您的令牌:

public async Task<LinkedInTokenResponse> RefreshToken(string yourRefreshToken, CancellationToken cancellationToken)
 {
     var client = new HttpClient();
     var request = new HttpRequestMessage(HttpMethod.Post, "https://www.linkedin.com/oauth/v2/accessToken");

     var parameters = new Dictionary<string, string>
     {
         { "grant_type", "refresh_token" },
         { "refresh_token", yourRefreshToken },
         { "client_id", _linkedInApplicationSettings.ClientId },
         { "client_secret", _linkedInApplicationSettings.PrimaryClientSecret }
     };

     request.Content = new FormUrlEncodedContent(parameters);

     var response = await client.SendAsync(request, cancellationToken);
     var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);
     response.EnsureSuccessStatusCode();

     var tokenResponse = JsonConvert.DeserializeObject<LinkedInTokenResponse>(responseContent);

     return tokenResponse;
 }

public class LinkedInTokenResponse
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("expires_in")]
    public int AccessTokenExpiresIn { get; set; }

    [JsonProperty("refresh_token")]
    public string RefreshToken { get; set; }

    [JsonProperty("refresh_token_expires_in")]
    public int RefreshTokenExpiresIn { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.