如何从API谷歌获取数据?

时间:2016-12-19 13:39:16

标签: google-api google-api-php-client google-play-games google-api-client

我有access_token,现在尝试使用此示例从Google API获取fata:

https://developers.google.com/android-publisher/api-ref/edits/details/get

我尝试将数据执行查询到此URL:

https://www.googleapis.com/androidpublisher/v2/applications/packageName/edits/editId

$curl = new Curl();
    $curl->get('https://www.googleapis.com/androidpublisher/v1/applications/com.shazam.android&access_token='.$_SESSION['access_token']["access_token"]);

$_SESSION['access_token']["access_token"]access_token

的位置

结果我得到消息:Error: 404: HTTP/1.1 404 Not Found

如何从API获取信息?

这是完整的网址请求,但不起作用:

https://www.googleapis.com/androidpublisher/v2/applications/com.shazam.android/edits/1?access_token=ya29.Ci-5Azg5JoBiESM5cEzM3TQrP3SXHAFirFg0f-Fj83PxtrtINWQDe2L-3f5wP7oAtQ

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

完整代码是:

// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();

$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    // Set the access token on the client.
    $client->setAccessToken($_SESSION['access_token']);


    $curl = new Curl();
    $curl->get('https://www.googleapis.com/androidpublisher/v1/applications/com.shazam.android/edits/1/&access_token='.$_SESSION['access_token']["access_token"]);

    if ($curl->error) {
        echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
    } else {
        echo 'Response:' . "\n";
        var_dump($curl->response);
    }

    // Create an authorized analytics service object.
    //$analytics = new Google_Service_Analytics($client);

    // Get the first view (profile) id for the authorized user.
    //$profile = getFirstProfileId($analytics);

    // Get the results from the Core Reporting API and print the results.
    //$results = getResults($analytics, $profile);
    //printResults($results);
} else {
    $redirect_uri = 'http://localhost/play/oauth2callback.php';
    header('Location: ' . $redirect_uri);
}

获得access_token后,我尝试发送Curl请求:

 $curl = new Curl();
        $curl->get('https://www.googleapis.com/androidpublisher/v1/applications/com.shazam.android/edits/1/&access_token='.$_SESSION['access_token']["access_token"]);

1 个答案:

答案 0 :(得分:0)

嗯,另一种可能由错误403 or Insufficient Permission引起的问题是您使用的范围不正确,或者您在检索访问令牌时没有请求所需的范围。因此,请尝试仔细检查您正在使用的范围,并启用您正在使用的开发控制台中的所有API。

您可以通过将access_token传递到此端点来检查您请求的范围:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ACCESS_TOKEN

有关详细信息,请查看这些相关SO问题中的解决方案是否可以帮助您。