简体   繁体   中英

How to get OAuth2 working with a service account

We need to migrate from the old provisioning API to the new Admin SDK. Problem is that we cannot figure out how to get OAuth2 to work with a service account. We are using the following code as the example, but cannot figure out where/how the key.p12 file is created. I have the service account created with a cliendId, email address, and API key tied to my server with the app on it in the developers console. If someone can help me to understand how to create the key.p12 file, then I am pretty sure I can get this working.

Thanks,

ski

// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'insert_your_client_id';
const SERVICE_ACCOUNT_NAME = 'insert_your_service_account_name';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '/super/secret/path/to/key.p12';

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();

... $key = file_get_contents(KEY_FILE); $client->setClientId(CLIENT_ID); $client->setAssertionCredentials(new Google_AssertionCredentials( SERVICE_ACCOUNT_NAME, array(' https://www.googleapis.com/auth/prediction '), $key) );

Go to https://cloud.google.com/console
Select your project
Click on 'APIs and Auth'
Click on 'Credentials'
Click on 'Create new Client ID'
Select 'Service Account'
Click 'Create Client ID'
Your private key will be downloaded.
Your public key will be stored and displayed in the console.
You will NOT be able to retrieve the private key again. Google only sends them once. If you lose it, you must do the process over again, and update the relevant settings in the application.
If this question has been answered, please mark it as such.

请参阅google oauth2如何获取服务帐户的私钥 -这实际上已随新版本的Google Cloud Console发生了变化,但是答案是最新的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM