首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“代码”:401,“消息”:“无效凭据”->Google驱动器连接错误

“代码”:401,“消息”:“无效凭据”->Google驱动器连接错误
EN

Stack Overflow用户
提问于 2022-01-21 12:49:13
回答 2查看 464关注 0票数 1

我有一个应用程序是连接到谷歌驱动器和传输数据到工作表。

我成功地建立了连接,一切都很完美,但是几个小时或几天后(取决于用户使用应用程序的服务器位置),我的连接中断了,我得到了以下消息:

“{”错误“:{”域“:”全局“,”原因“:"authError",”消息“:”无效凭据“,"locationType":”标头“,”位置“:”授权“},”代码“:401,”消息“:”无效凭据“}”。

以下是代码:

代码语言:javascript
复制
    $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();

                }

在我刷新连接后,一切都会再次正常工作,并再次出现问题。

是否知道,为什么会发生?

致以敬意,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-02 08:03:18

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

代码语言:javascript
复制
 } 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();
                 

                }

现在应用程序工作得很巧妙,正常的断开连接!

票数 0
EN

Stack Overflow用户

发布于 2022-01-21 19:08:05

正在测试中的应用程序七天后刷新令牌就过期了。

将您的应用程序修复为生产。

刷新令牌

项目的OAuth同意屏幕配置为外部用户类型,发布状态为“测试”,该项目将在7天内到期。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70801543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档