ski98033
ski98033

Reputation: 1

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

Upvotes: 0

Views: 1329

Answers (2)

HardScale
HardScale

Reputation: 1021

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.

Upvotes: 1

aeijdenberg
aeijdenberg

Reputation: 2457

See google oauth2 how to get private key for service account - this actually changed with a new version of Google Cloud Console today, however the answer there is up to date.

Upvotes: 0

Related Questions