首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当访问google电子表格时,我得到“您需要权限”

当访问google电子表格时,我得到“您需要权限”
EN

Stack Overflow用户
提问于 2021-11-03 09:10:11
回答 1查看 134关注 0票数 1

我有一个应用程序,当我尝试在Google电子表格上连接时,在连接过程中,我得到了消息“你需要许可”!

我使用下面的代码来getSpredsheets:

代码语言:javascript
复制
try{
            $spreadsheetFeed = null;
            
            $auth_url = '';
            
            $db->setQuery("Select password From #__breezingforms_addons_gdata Where form_id = " . intval($form_id));
         $accessToken = $db->loadResult();
          // $accessToken='{"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","scope":"https:\/\/www.googleapis.com\/auth\/spreadsheets","token_type":"Bearer","created":434345842294}';

            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();
                    }
                    
                    $serviceRequest = new DefaultServiceRequest($token->access_token, $token->token_type);
                    ServiceRequestFactory::setInstance($serviceRequest);

                    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
                    $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
                    
                }catch(Exception $ee){
                    
                  //$accessToken = null;
                    //$auth_url = $this->client->createAuthUrl();
          $error=$ee->getMessage();
                              
                              }
            }

            if($spreadsheetFeed !== null){
                foreach($spreadsheetFeed As $sheet){
                    $gdata_spreadsheets[$sheet->getId()] = $sheet->getTitle();
                }
            }
            
            if($gdata->spreadsheet_id != '' && isset( $gdata_spreadsheets[$gdata->spreadsheet_id] ) && $spreadsheetFeed !== null){

                $spreadsheet = $spreadsheetFeed->getByTitle($gdata_spreadsheets[$gdata->spreadsheet_id]);
                $worksheetFeed = $spreadsheet->getWorksheets();
                
                foreach ( $worksheetFeed as $sheet ){
                    $gdata_worksheets[$sheet->getId()] = $sheet->getTitle();
                }
                
                if($gdata->worksheet_id != '' && isset( $gdata_worksheets[$gdata->worksheet_id] )){
                    
                    $worksheet = $worksheetFeed->getByTitle($gdata_worksheets[$gdata->worksheet_id]);
                    $cellFeed = $worksheet->getCellFeed();

                    foreach($cellFeed->getEntries() as $cellEntry) {
                        
                        $row = $cellEntry->getRow();
                        $col = $cellEntry->getColumn();
                        
                        if( $row > 1 ){
                            break;
                        }
                        
                        $gdata_columns[] = $cellFeed->getCell($row, $col)->getContent();
                        
                    }
                }
            }
        
        } catch(Exception $e){
            
            $error = $e->getMessage();
        }
    //}

在这段代码中:

代码语言:javascript
复制
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();

类getSpreadsheets()用于获取电子表格的数量,类的内容为:

代码语言:javascript
复制
public function getSpreadsheets()
{
    return new SpreadsheetFeed(
        ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full')
    );
}

我总是收到这样的信息:"you need permission",我是谷歌驱动器上电子表格的所有者,我不知道为什么我会收到这个错误信息?

我想问题是站内Google账号有权限,但是我找不到哪里可以修改呢?是否有人知道是什么导致了这种情况?

如果我想访问id为的电子表格:

代码语言:javascript
复制
 public function getSpreadsheetById($id)
{
        
    return new Spreadsheet(
        new SimpleXMLElement(
                            
            ServiceRequestFactory::getInstance()->get('feeds/spreadsheets/private/full/'. $id)
        )
    );
}

然后调用该方法:

代码语言:javascript
复制
   $serviceRequest = new DefaultServiceRequest($token->access_token, $token->token_type);
                    ServiceRequestFactory::setInstance($serviceRequest);

                    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
                    //$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
                    $spreadsheetFeed = $spreadsheetService->getSpreadsheetById("1Bu6CjCDN-cjvkyE8F-YyI8V73Zz8a7NkQibKB5Nfirg");

正如我所看到的,我在我的驱动器上找到了那个电子表格,但我无法访问她,“你需要许可”。

EN

回答 1

Stack Overflow用户

发布于 2021-11-03 17:20:45

我认为你完全误解了Oauth2是如何工作的。

你一直在问Why I can see in my Google account that my application has permission to read.

这不是Oauth2的工作方式。默认情况下,您的应用程序无法访问任何内容。它不是用户。

当您的应用程序首次运行时。它将显示登录和同意屏幕,以供运行您的应用程序的用户使用。如果他们接受您的同意请求,您将能够访问用户帐户。在这种情况下,用户的驱动器帐户。

这是设置oauth2客户端的代码。当代码运行时,它将请求用户同意访问Google drive。我可以看到,由于请求的作用域是"https://www.googleapis.com/auth/drive"

代码语言:javascript
复制
$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google\Service\Drive($client);

当此代码运行时,它将请求用户的权限,并返回访问令牌。该访问令牌将授予对用户数据的访问权限。

因此,无论您的访问令牌是如何创建的,这都是您可以访问的帐户。请记住,访问令牌仅在一个小时内有效。

如果您使用该访问令牌运行about.get,您应该能够看到谁是该驱动器帐户的所有者。

您还可以运行file.list来查看该用户有权访问哪些文件。

"You need permission"!通常意味着用户无权读取该文件。但正常情况下,它将是一个未找到的404文件。

因此用户没有访问该文件的权限。

我认为你在做什么。

在我看来,您确实有一个标准文件,您正在尝试让您的应用程序读取和访问您的用户。您应该考虑使用服务帐户。

这个视频解释了什么是服务帐户:Should you be using a Google service account after in 2021?

这将向您展示如何创建Service account credetinals

下面的代码显示了服务帐户身份验证的ServiceAccount.php工作原理

代码语言:javascript
复制
require_once __DIR__ . '/vendor/autoload.php';

// Use the developers console and download your service account
// credentials in JSON format. Place the file in this directory or
// change the key file location if necessary.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');

/**
 * Gets the Google client refreshing auth if needed.
 * Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
 * Initializes a client object.
 * @return A google client object.
 */
function getGoogleClient() {
    return getServiceAccountClient();
}

/**
 * Builds the Google client object.
 * Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts
 * Scopes will need to be changed depending upon the API's being accessed. 
 * array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
 * List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
 * @return A google client object.
 */
function getServiceAccountClient() {
    try {   
        // Create and configure a new client object.        
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope([YOUR SCOPES HERE]);
        return $client;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69822225

复制
相关文章

相似问题

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