dsv768456 2016-04-20 01:45
浏览 71
已采纳

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

This has been a nightmare to get right for the past few days. First I was struggling with redirect_uri_mismatch errors, or bad requests, but now that I thought I nailed it, I'm getting this message from Google after clicking the "Allow" button in the consent screen :

Please copy this code, switch to your application and paste it there

Where exactly do I need to paste this code? I'm using PHP in a web server, I went to the "Other" application type when creating the credentials, because I read that this was preferred if I didn't want my users to keep getting that auth link.

I can't seem to find a concrete example of how to do this, I got it working this far by grabbing bits from here and there, but this one I just can't figure it out.

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

I'm using a file client_secret.json that I downloaded from Google's oAuth credentials screen, it looks like this:

{"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"]}}

Anyone knows what else I need to do or where to put that code returned from Google after the consent screen?

Thanks

  • 写回答

2条回答 默认 最新

  • duanrui3480 2016-04-23 19:49
    关注

    Although the answer here does not use PHP, I still think it's worth to add it here as it's the only complete working example of how to update a youtube video without displaying the consent screen to the user, at least I wasn't able to find a concrete working example.

    I ended up using the Javascript library, I still wasn't able to find a single complete example, not even in the library docs, nor Google's, it took taking pieces of code from here and there, and connecting the dots.

    First thing to do is to create the credentials, we do this by going to Google developer console, and under Credentials we create a new OAuth Client ID, choosing Web Application and adding our domain to the Authorized JavaScript origins field - eg. http://www.example.com

    The only information we will need from these credentials is the Client ID, so we copy it and paste it in the Javascript file below.

    Here's the HTML part, where we also load the API library:

    <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>
    

    This is the Javascript code to make it happen - update.js in the code above, remember to update with your own client ID: https://gist.github.com/andruxnet/2efe7898f5cd9722e0d42b71fce5d183

    The relevant Javascript code, once we figure out the authentication part that can be found in several places online, is this piece of code:

    // 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();
    });
    

    I hope this saves someone's time in the future as it would have saved my own.

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?