Join Joseph Lowery for an in-depth discussion in this video Authorizing requests, part of Google Cloud Compute Engine Essential Training.
- Time to bring in our authorization routine. We'll use Google API functions to apply our project ID, client ID, and browser based public API access so that we can programmatically work with Compute Engine. I have open in my code editor the HTML page I started to work on in the previous lesson. You can find the same starting point in the chapter 4 04_02 start folder. Also there's a file named auth_script.inc that contains the authorization code we'll be using.
So, I'll open that up so we can take a look. Up top are the initial variables that we'll use to fill out the values I just mentioned from our Google Cloud console. We'll put those in in just a moment. Let's go through the authorization function first. That starts on line 10. And first, we use one of its functions to adjust the client and set the API key and of course, we're passing on the variable API_KEY that we'll customize.
Next, we use another function, Google API .auth.authorize and pass in the client ID, the scope, which as you can see from the variable above limits us to working with the Compute Engine and there's another flag called immediate. By setting this mode to true we ensure that the pop up authorizing consent is displayed. We will at a later point change that flag to false to avoid showing the pop up again and again while testing.
The next part, simply handles the result that comes back from that authorization, and if it's successful it shows a little alert. It says Authorization Success, and if it's not successful we get a failed message. And then right below that I have jQuery document ready function that simply loads and runs the authorization function. All right, so let me copy this entire script, go over to my working file, and I'll paste this in the head document right after our other script declarations.
Now, let's fill out the variables which now start on line 9. First, we're gonna wanna get the project ID. So, back to the console and I'm gonna go to my projects page and there you can see the project ID. In this case, it's isotropic-snow-771. Yours is gonna be unique and different. I'll copy that, go to my code editor, and then plug that in as the value for project ID.
Next, client ID. We're gonna find this one on the credentials page and this'll be the client ID for the web application that we set up previously. So, again, I'll copy that value and plug it into my code. Finally, we're gonna need to get the API key. Again, this is handled through the credentials page and I'll scroll down and you'll find this in the key for browser applications.
This is the public API access we set up in the previous chapter. Now, before you copy your API key, if you haven't worked with this in a little bit of time, you may need to regenerate the key. As you can see, mine is currently active. If you don't see the active indicator here click Regenerate Key, and then copy that value. The keys are only active for twenty four hours so you might need to make this change if you're working with this project over a couple of days.
All right, I've copied that and now I'll replace the placeholder. Okay, I'll save my file and we're ready to upload. Now, I'll just hit my up arrow in terminal to repeat the copy files command. Hit enter and the file's posted. Now, let's go to the browser. Now, before I click the refresh button I do want to note that I have enabled pop ups on my version of Chrome for this site.
- Integrating with Google Cloud products
- Setting up the Google Cloud SDK
- Creating a Compute Engine instance
- Authenticating users
- Working with Python and Compute Engine
- Managing resources
- Implementing network load balancing