Quantcast
Viewing latest article 2
Browse Latest Browse All 2

How can I implement GMail OAuth 2.0 on server using PHP?

How can I implement OAuth 2.0 on server using PHP to create/update email accounts for our domain. Most examples use $CLIENT_SECRET witch I don't have, may be it's old way or something. Here is a code I wrote:

createEmail('azzozhsn@domain', 'password', 'Azzoz', 'Al-hasani');function createEmail($username, $password, $firstName, $lastName){$p12key             = file_get_contents('file.p12');$APIkey             = 'XXXXXXXXXXXXX';$clientId           = 'YYYYYYYYYYcipbi.apps.googleusercontent.com';$emailAddress       = 'ZZZZZZZZZZ@developer.gserviceaccount.com';$certFingerprints   = 'WWWWWWWWWWW';$user2impersonate   = 'admin@domain';require_once('Google/autoload.php');$scopes = array('https://www.googleapis.com/auth/admin.directory.user');$cred = new Google_Auth_AssertionCredentials(     $clientId,     $scopes,     $p12key);$cred->sub = $user2impersonate;$client = new Google_Client();$client->setClientId($clientId);$client->addScope("https://www.googleapis.com/auth/admin.directory.user");$client->setAssertionCredentials($cred);    $user = new Google_Service_Directory_User();$name = new Google_Service_Directory_UserName();$name->setFamilyName($lastName);$name->setGivenName($firstName);$user->setName($name);$user->setHashFunction("MD5");$user->setPrimaryEmail($username);$user->setPassword(hash("md5", $password));$user->setExternalIds(array("value"=>28790,"type"=>"custom","customType"=>"EmployeeID"));$service = new Google_Service_Directory($client);$result = $service->users->insert($user);return $result;}

then with some debugging I Found this message:

{"error": {"errors": [   {"domain": "global","reason": "authError","message": "Invalid Credentials","locationType": "header","location": "Authorization"   }  ],"code": 401,"message": "Invalid Credentials" }}

I know the problem is in the authentication but I don't know how to do it, most codes I found use client_secret witch I don't have. It like old method or something...


Viewing latest article 2
Browse Latest Browse All 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>