Daria
Daria

Reputation: 861

Can't get access from my site to Google Drive API

I have a website that isn't allowed out of our network. We use some domain name to access to it from our company's network (say desire.it.loc). And now I need to upload some file to google drive from my site. As I got so far first I should do is to authenticate to google account. I created project at google developers console, got client_id, secret etc. And now I'm getting

Error: invalid_request
device_id and device_name are required for private IP: http://x.x.x.x:x

My php code:

session_start();
$client = new Google_Client();
$client->setApplicationName("limits");
$client->setDeveloperKey("<key goes here>");    
$client->setClientId('<id goes here>');
$client->setClientSecret('<secret goes here>');    
$client->setRedirectUri('http://x.x.x.x:x');
$client->setScopes(array('http://x.x.x.x:x'));    

if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
}    

if (isset($_GET['code'])) {    
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}    

if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
    $authUrl = $client->createAuthUrl(); // error goes here
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}    

if (isset($_SESSION['token'])) {
    print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
    $client->setAccessToken($_SESSION['token']);
    $service = new Google_Service_Analytics($client);    

    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();

    foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> \n";
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> \n";

            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                    //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> \n";
                }
            }
        }
    } // closes account summaries

}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>

I'm not the administrator of our web-server, so I guess it would be difficult for me to change some setting there. Can I fix it somehow without registering "fake domains" or something like this.

Upvotes: 0

Views: 158

Answers (1)

pinoyyid
pinoyyid

Reputation: 22296

$client->setRedirectUri('http://x.x.x.x:x'); isn't allowed. You need a domain name such as dev.example.com. You can configure your /etc/hosts (or windows equivalent) to resolve dev.example.com to the ip address. The same domain name needs to be registered in the Google API Console.

Upvotes: 1

Related Questions