【问题标题】:Get downloadable url from github zipball url从 github zipball url 获取可下载的 url
【发布时间】:2020-07-27 09:08:15
【问题描述】:
if ( is_null( $this->github_response ) ) { // Do we have a response?
        $request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $this->username, $this->repository ); // Build URI

        if( $this->authorize_token ) { // Is there an access token?
           /* $request_uri = add_query_arg( 'access_token', $this->authorize_token, $request_uri ); // Append it*/



            $basicauth = 'token ' . $this->authorize_token;
            $headers = array( 
                    'Authorization' => $basicauth
            );
        }

        $pload = array(
            'headers' => $headers
            );
        $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri, $pload ) ), true ); // Get JSON and parse it

        if( is_array( $response ) ) { // If it is an array
            $response = current( $response ); // Get the first item
        }

        if( $this->authorize_token ) { // Is there an access token?
            $response['zipball_url'] = add_query_arg( 'access_token', $this->authorize_token, $response['zipball_url'] ); // Update our zip url with token
        }

        $this->github_response = $response; // Set it to our property
    }

我的目标是从最新的 git 版本中获取可下载的 url。我还使用此代码获得了可下载的 url。但问题是 github 向我发送一封关于已弃用问题的电子邮件:Deprecating API authentication through query parameters。

我阅读了 github 的文档,但无法解决。谁能帮帮我?

【问题讨论】:

  • 也对此感兴趣。
  • 您是否能够弄清楚如何更新代码并在标头中发送令牌?
  • 是的,我能够解决问题

标签: php wordpress github


【解决方案1】:

我在 cURL 请求中达到了这一点:

curl_setopt_array($curl, [
    CURLOPT_URL => $request_uri,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => [
        "Authorization: token " . $this->authorize_token,
        "User-Agent: My App Name Here"
    ]
]);

但是如果我注释掉这一行:

// if ($this->authorize_token) {
//     $response['zipball_url'] = add_query_arg('access_token', $this->authorize_token, $response['zipball_url']);
// }

我收到“未找到”错误。

【讨论】:

    【解决方案2】:

    GitHub 不推荐使用 access_token 查询参数访问 API。这种方法的问题在于它会导致令牌记录在各种日志中(不是 GitHub 的,而是几乎所有其他人的),这是一个巨大的安全问题。

    如果您想发出 API 请求,您应该以 token TOKEN 的形式在 Authorization 标头中传递令牌,其中 TOKEN 是您的令牌值,根本没有在网址中。在您的代码中,您将 access_token 参数添加到 zipball URL,您不应该这样做;您应该使用 Authorization 标头,就像您对初始请求所做的那样。如有必要,您还应该准备好遵循重定向。

    【讨论】:

    • 感谢您的宝贵回复。我理解程序,但无法处理代码。能否请您看一下我的代码并告诉我如何完成它?
    • 我对 PHP 不是很熟悉。我可以看出你的代码出了什么问题,而且我对 GitHub 实现的这方面非常熟悉,但是如果没有大量研究和全套工作代码,我不知道如何编写 PHP 来修复你的代码。跨度>
    猜你喜欢
    • 2015-11-20
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 2018-10-05
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多