在我使用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?

在过去的几天里,这是一场噩梦。起初,我一直在努力解决重定向_uri_mismatch错误或错误请求,但现在我认为我已经搞定了,我在点击同意屏幕上的"允许"按钮后从谷歌收到了这条消息:

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

我到底需要把这个代码粘贴到哪里?我在web服务器中使用PHP,在创建凭据时,我选择了"其他"应用程序类型,因为我读到,如果我不想让我的用户继续获得授权链接,这是首选。

我似乎找不到一个具体的例子来说明如何做到这一点,我通过从这里和那里抓取一些东西来实现它,但这一个我就是想不通。

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

我使用的是从谷歌的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"]}}

有人知道我还需要做什么,或者在同意屏幕后把谷歌返回的代码放在哪里吗?

感谢

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

我最终使用了Javascript库,但我仍然找不到一个完整的例子,即使在库文档中,也没有谷歌的文档中,它需要从这里和那里提取代码片段,并将这些点连接起来。

首先要做的是创建凭据,我们通过转到Google开发人员控制台来完成,在凭据下,我们创建一个新的OAuth客户端ID,选择Web应用程序并将我们的域添加到Authorized JavaScript originals字段中。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();
});

我希望这能在未来节省别人的时间,就像它能节省我自己的时间一样。

干杯

我看到虚假信息,去查看这个链接:

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

你有很多请求的例子,不管怎么说,你从谷歌收到的代码是Auth code,它允许你对客户端进行身份验证(更确切地说,是在你的php应用程序中创建的Google_client->authenticate(AuthCode)),之后你可以开始使用他们的Accesstoken或RefreshTokens服务,这取决于你请求生成perms的连接类型。

我希望这条评论能帮助你摆脱开发谷歌服务的噩梦:)