【问题标题】:"code": 401, "message": "Invalid Credentials" ->Google drive connection error"code": 401, "message": "Invalid Credentials" -> Google 驱动器连接错误
【发布时间】:2022-01-21 12:49:13
【问题描述】:

我有一个连接到 Google 驱动器并将数据传输到工作表的应用程序。

我成功建立了连接,一切正常,但几个小时或几天后(取决于用户使用应用程序的服务器),我的连接断开了,我收到了这条消息:

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

下面是代码:

    $this->client = new Google_Client();
    $this->client->setApplicationName('BreezingForms Google Drive Spreadsheets');
    $this->client->addScope(array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets'));

    // testing:
    // 197794184197-bt2q9knrdu1i54vgladd97ob196k4c6s.apps.googleusercontent.com
    // dImciIWj3WNOrIcYRbu9MFeA

    if (isset($_POST['gdata_custom_client_id']) && trim($_POST['gdata_custom_client_id']) != '' && trim($_POST['gdata_custom_client_secret']) != '') {

        $this->client->setClientId(trim($_POST['gdata_custom_client_id']));
        $this->client->setClientSecret(trim($_POST['gdata_custom_client_secret']));

        $db->setQuery("Update #__breezingforms_addons_gdata Set custom_client_id = " . $db->quote(trim($_POST['gdata_custom_client_id'])) . ", custom_client_secret = " . $db->quote(trim($_POST['gdata_custom_client_secret'])) . " Where form_id = " . intval($_REQUEST['form']));
        $db->execute();

    } else {

        $form_id = -1;

        if(JRequest::getInt('ff_form',-1) > 0){

            $form_id = JRequest::getInt('ff_form',-1);

        }else if(isset($_REQUEST['form'])){

            $form_id = intval($_REQUEST['form']);
        }

        $db->setQuery("Select * From #__breezingforms_addons_gdata Where form_id = " . $db->quote($form_id));
        $client = $db->loadObject();

        if ($client) {

            $this->client->setClientId($client->custom_client_id);
            $this->client->setClientSecret($client->custom_client_secret);
        }
    }

    $this->client->setApprovalPrompt('auto');
    $this->client->setPrompt('consent');
    $this->client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
    $this->client->setAccessType('offline');


}

function onPropertiesDisplay($form_id, $tabs){
    
    if(!$form_id) return '';
    
    $error = '';
    
    $db = JFactory::getDBO();
    
    $db->setQuery("Select `title`,`name`,`id` From #__facileforms_elements Where form = " . intval($form_id) . " And `title` Not In ('bfFakeTitle','bfFakeTitle2','bfFakeTitle3','bfFakeTitle4','bfFakeTitle5') And `type` Not In ('','UNKNOWN') Order By ordering");
    $breezingforms_fields = $db->loadObjectList();
    
    $db->setQuery("Select `enabled`, `username`, `password`, `worksheet_id`, `spreadsheet_id`, `fields`, `meta`, `debug` From #__breezingforms_addons_gdata Where form_id = " . intval($form_id));
    $gdata = $db->loadObject();
    
    if( $gdata === null ){
        $gdata = new stdClass();
        $gdata->username = '';
        $gdata->password = '';
        $gdata->enabled = 0;
        $gdata->worksheet_id = '';
        $gdata->spreadsheet_id = '';
        $gdata->fields = '';
        $gdata->meta = '';
        $gdata->debug = 0;
    }
    
    $gdata->fields = explode('/,/', $gdata->fields);
    $gdata->meta   = explode('/,/', $gdata->meta);
    
    $gdata_spreadsheets = array();
    $gdata_worksheets = array();
    $gdata_columns = array();
    $worksheets_name=array();
               $worksheets_name1=array();
               $worksheets_name2=array();

    
    //if( $gdata->enabled == 1 ){
        
        try{
        
            $spreadsheetFeed = null;
            
            $auth_url = '';
            
            $db->setQuery("Select password From #__breezingforms_addons_gdata Where form_id = " . intval($form_id));
            $accessToken = $db->loadResult();

            if(!$accessToken){
                
                $auth_url = $this->client->createAuthUrl();
                
            } else {
                
                try{
                    
                    $this->client->setAccessToken($accessToken);
                    $token = json_decode($accessToken);
            
                    if ($this->client->isAccessTokenExpired()) {
                        $this->client->refreshToken($token->refresh_token);
                         $tok = json_encode($this->client->getAccessToken());
                        $token = json_decode($tok);
                        $db->setQuery("Update #__breezingforms_addons_gdata set password = " . $db->quote($tok) . " Where form_id = " . intval($form_id));
                        $db->execute();
                    }
             
                }catch(Exception $e){
                    
                    $accessToken = null;
                    $auth_url = $this->client->createAuthUrl();
                    //$error = $e->getMessage();

                }

刷新连接后,一切正常,问题又出现了。

是否知道,为什么会这样?

问候,

【问题讨论】:

  • 究竟是多长时间但过了几天或几天请编辑您的问题并包含您的代码
  • 嗨,几个小时或几天后,取决于用户使用该应用程序的服务器,我的应用程序在世界各地使用,在不同的服务器上,用户报告该问题,并且当然,我也注意到了这个问题。 tnx :)
  • 1.你能验证刷新令牌是否设置在 $token = json_decode($accessToken); 2. 您能否验证您是否始终存储最新的刷新令牌。 3. 你能验证你的应用是在生产中,而不是在谷歌云控制台中测试吗?
  • 嗨,是的,我可以确认你所有的问题,除了最后,我的应用在谷歌云控制台中处于“测试”状态。这会导致这个问题吗?我对此表示怀疑! tnx :)
  • 您是否检查过这是否是问题的原因? Refresh token expiration

标签: php google-drive-api google-oauth google-api-php-client


【解决方案1】:

正在测试的应用的刷新令牌会在 7 天后过期。

将您的应用程序修复到生产环境中。

Refresh token

为外部用户类型配置了 OAuth 同意屏幕且发布状态为“测试”的 Google Cloud Platform 项目会发出一个在 7 天后到期的刷新令牌。

【讨论】:

  • 嗨@Dalm To,非常感谢您的解释!一如既往的好!最好的问候!
【解决方案2】:

问题出在代码中 -> 获取刷新令牌的方式错误,正确的代码是:

 } else {
                
                try{
                    $token = json_decode($accessToken, true);
                    $this->client->setAccessToken($token);
              

                if ($this->client->isAccessTokenExpired()) {
                         if ($this->client->getRefreshToken()) {
                 $this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
    }
               
                    }
             
                }catch(Exception $e){
                    
                    $accessToken = null;
                    $auth_url = $this->client->createAuthUrl();
                 

                }

现在应用程序可以完美运行,不再断开连接!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2018-09-10
    • 2014-04-07
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多