Giter Site home page Giter Site logo

https://oauth2.googleapis.com/token curl: (28) Failed to connect to oauth2.googleapis.com port 443 after 227030 ms: Couldn't connect to server about google-api-php-client HOT 1 CLOSED

wenshan avatar wenshan commented on May 23, 2024
https://oauth2.googleapis.com/token curl: (28) Failed to connect to oauth2.googleapis.com port 443 after 227030 ms: Couldn't connect to server

from google-api-php-client.

Comments (1)

yash30201 avatar yash30201 commented on May 23, 2024

Hi @wenshan, thanks for raising this issue. The "Failed to connect" error (code 28) usually indicates a network-level problem rather than an issue with the code (and thus this library) itself. I've tried reproducing it with curl as well as using PHP curl and didn't face any issue.

Code
<?php

use Firebase\JWT\JWT;

require_once __DIR__ . '/../vendor/autoload.php';

$key = getSignedJwtClaim();
sendCurlRequest($key);

function getSignedJwtClaim()
{
    $jwtClaims = [
        "iss" => getenv('SERVICE_ACCOUNT_EMAIL'),
        "scope" => "https://www.googleapis.com/auth/devstorage.read_only",
        "aud" => "https://oauth2.googleapis.com/token",
        "exp" => time() + 3600,
        "iat" => time()
    ];

    $key = json_decode(file_get_contents(getenv('GOOGLE_APPLICATION_CREDENTIALS')), true);
    return JWT::encode(
        $jwtClaims,
        $key['private_key'],
        'RS256'
    );
}

function sendCurlRequest($key) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://oauth2.googleapis.com/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded',
    ]);
    curl_setopt(
        $ch,
        CURLOPT_POSTFIELDS,
        'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=' . $key
    );

    $response = curl_exec($ch);
    $response = json_decode($response, true);
    echo "Access token => " . $response['access_token'] . PHP_EOL;
    curl_close($ch);
}

You can try adding -v flag to you curl command to get more verbose output and get some clue as to why it's happening on your system. It's very helpful that you've confirmed it works with Postman. This reinforces the idea that the issue likely lies in the network configuration on the machine where cURL is running.

As there isn't anything actionable in this library, hence closing this issue.

from google-api-php-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.