【问题标题】:How to get data from API google?如何从 API google 获取数据?
【发布时间】:2017-05-04 13:55:24
【问题描述】:

我有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 中获取信息?

这是完整的 URL 请求,它不起作用:

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"]);

【问题讨论】:

  • 您的获取请求缺少来自请求“edits/editId”developers.google.com/android-publisher/api-ref/edits/get 的最后一个参数
  • 这个参数应该包含什么内容?
  • 如果您将其正确格式化为获取请求,它可能吗? != &
  • 我用正确的查询更新了问题
  • Insufficient Permission 告诉您问题出在哪里,然后您进行身份验证的用户无权查看。

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


【解决方案1】:

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

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

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

有关更多信息,请查看这些相关 SO 问题中的解决方案,如果它可以帮助您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多