在我使用 PHP 授予对已安装应用程序的访问权限后,我必须将 Google oAuth 返回的代码放在哪里?

Where do I have to place the code returned by Google oAuth after I grant access to an installed application, using PHP?

在过去的几天里,这一直是一场噩梦。起初,我遇到了 redirect_uri_mismatch 错误或错误请求,但现在我认为我成功了,在点击同意屏幕中的 "Allow" 按钮后,我从 Google 收到了这条消息:

请复制此代码,切换到您的应用程序并将其粘贴到那里

我需要将此代码粘贴到什么地方?我在 Web 服务器中使用 PHP,我在创建凭据时转到了 "Other" 应用程序类型,因为我读到如果我不希望我的用户继续获得该身份验证,这是首选link.

我似乎找不到一个具体的例子来说明如何做到这一点,我通过到处抓取一些位来让它工作到现在,但是我就是想不通。

https://gist.github.com/andruxnet/0f7fe237730c13846a690da12935a708

我正在使用从 Google 的 oAuth 凭据屏幕下载的文件 client_secret.json,它看起来像这样:

{"installed":{"client_id":"xxxxxxxxxxxxxxx.apps.googleusercontent.com","project_id":"my-project-id","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxxxxxxxxx","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}

任何人都知道我还需要做什么或在同意屏幕后将从 Google 返回的代码放在哪里?

谢谢

我看到虚假信息了,去看看这个link:

https://developers.google.com/api-client-library/php/auth/web-app#example

你有很多请求的例子,无论如何你从 google 收到的代码是允许你验证客户端的授权代码(Google_client->authenticate(AuthCode) 在你 php 应用程序),之后您可以开始使用他们的服务与 Accesstoken 或 RefreshTokens,具体取决于您请求生成权限的连接类型。

希望此评论对您开发 Google 服务的噩梦有所帮助 :)

虽然这里的答案没有使用 PHP,但我仍然认为值得在此处添加它,因为它是如何在不向用户显示同意屏幕的情况下更新 YouTube 视频的唯一完整工作示例,至少我找不到具体的工作示例。

我最终使用了 Javascript 库,我仍然无法找到一个完整的示例,甚至在库文档中也没有,也没有在 Google 的文档中找到,它花了很多时间来自各处的代码,以及连接点。

首先要做的是创建凭据,我们通过转到 Google 开发人员控制台来完成此操作,在凭据下我们创建一个新的 OAuth 客户端 ID,选择 Web 应用程序并将我们的域添加到授权JavaScript 来源字段 - 例如。 http://www.example.com

我们需要从这些凭据中获取的唯一信息是客户端 ID,因此我们将其复制并粘贴到下面的 Javascript 文件中。

这是 HTML 部分,我们还加载了 API 库:

<input type="button" id="make-private" value="private" /> Make video private
<input type="button" id="make-public" value="public" /> Make video public
Current privacy status: <span id="current-status"></span>
<script type="text/javascript" src="update.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>

这是实现它的 Javascript 代码 - update.js 在上面的代码中,记得更新您自己的客户端 ID:https://gist.github.com/andruxnet/2efe7898f5cd9722e0d42b71fce5d183

相关的Javascript代码,弄清楚网上好几个地方都能找到的认证部分,是不是这段代码:

// we first grab the video resource from Youtube, using youtube video id
var request = gapi.client.youtube.videos.list({
    id: 'youtubevideoid',
    part: 'status'
});

// once we get the video resource, we update the status and 
// run the update method
request.execute(function(response) {
    // since we looked for a video id, we only got one result - the first 
    // and only item in the array of result items
    video = response.result.items[0];

    // set the privacy status to one of public, private or unlisted
    video.status.privacyStatus = 'private';

    // prepare the update with the new privacy status
    var updateRequest = gapi.client.youtube.videos.update({
        part: 'status',
        fields: 'status',
        resource: video
    });

    // execute the update - I didn't see this part in the API documentation, 
    // I found it somewhere else in SO as part of another question, although 
    // I should've figured it out by looking at the first list() method above
    updateRequest.execute();
});

我希望这会在将来节省其他人的时间,因为它会节省我自己的时间。

干杯