Google API クライアントによるトークンの更新
Google Analytics API (V3) の利用時、特に取得しようとしたときにエラーが発生する場合があります。テストアカウント以外のアカウントからのデータ。この問題は通常、403 エラーとして現れます。さらに、トークンを取り消して再認証した後でも、トークンの有効期限が切れると問題が再発する可能性があります。
この問題に対処する解決策は、refreshToken メソッドを利用して新しい認証トークンを取得することです。ただし、$client->refreshToken(refresh_token_key) を実行すると、無効な付与エラー メッセージ ("error" : "invalid_grant") が生成される場合があります。
refreshToken メソッドの背後にあるコードを調査すると、次のことが明らかになります。パラメータが送信される正しく:
- client_id
- client_secret
- refresh_token
- grant_type (refresh_token としてハードコードされています)
トークンリフレッシュの手順は次のとおりです
$client = new apiClient(); $client->setClientId($config['oauth2_client_id']); $client->setClientSecret($config['oauth2_client_secret']); $client->setRedirectUri($config['oauth2_redirect_uri']); $client->setScopes('https://www.googleapis.com/auth/analytics.readonly'); $client->setState('offline'); $client->setAccessToken($config['token']); // The access JSON object. $client->refreshToken($config['refreshToken']); // Will return error here
問題を解決する鍵は、認証時に取得されたこの初期トークンがリフレッシュ トークンを所有していることを理解することです。最初のトークンは 1 時間以内に期限切れになりますが、リフレッシュ トークンを利用して新しいトークンを取得できます。ただし、この一時トークンも 1 時間後に期限切れになり、関連付けられた更新トークンがないことに注意することが重要です。したがって、新しい一時トークンを取得するには、初期トークンのリフレッシュ トークンを使用する必要があります。
わかりやすくするために、次のコードで詳細な説明を示します。
// Retrieve original token from database $tokenquery = "SELECT * FROM token WHERE type='original'"; $tokenresult = mysqli_query($cxn, $tokenquery); if ($tokenresult != 0) { $tokenrow = mysqli_fetch_array($tokenresult); extract($tokenrow); } $time_created = json_decode($token)->created; $t = time(); $timediff = $t - $time_created; $refreshToken = json_decode($token)->refresh_token; // Initialize Google client $client = new Google_Client(); $client->setApplicationName(''); $client->setScopes(array()); $client->setClientId(''); $client->setClientSecret(''); $client->setRedirectUri(''); $client->setAccessType('offline'); $client->setDeveloperKey(''); // Refresh token if expired if (($timediff > 3600) && ($token != '')) { $refreshquery = "SELECT * FROM token WHERE type='refresh'"; $refreshresult = mysqli_query($cxn, $refreshquery); // If refresh token exists if ($refreshresult != 0) { $refreshrow = mysqli_fetch_array($refreshresult); extract($refreshrow); $refresh_created = json_decode($token)->created; $refreshtimediff = $t - $refresh_created; // If refresh token is also expired if ($refreshtimediff > 3600) { $client->refreshToken($refreshToken); $newtoken = $client->getAccessToken(); $tokenupdate = "UPDATE token SET token='$newtoken' WHERE type='refresh'"; mysqli_query($cxn, $tokenupdate); $token = $newtoken; echo "refreshed again"; } else { $client->setAccessToken($token); echo "use refreshed token but not time yet"; } } else { $client->refreshToken($refreshToken); $newtoken = $client->getAccessToken(); $tokenupdate = "INSERT INTO token (type, token) VALUES ('refresh', '$newtoken')"; mysqli_query($cxn, $tokenupdate); $token = $newtoken; echo "refreshed for first time"; } } // If token is still valid if (($timediff setAccessToken($token); } $service = new Google_DfareportingService($client);
以上がGoogle Analytics APIでトークンを更新する際の「invalid_grant」エラーを解決するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

thesecrettokeepingaphp-poweredwebsterunningsmootlyunderheavyloadinvolvesseveralkeystrategies:1)emform opcodecoduceSciptionexecutiontime、2)aatabasequerycachingwithiThing withiThistolessendavasoload、

コードをより明確かつ維持しやすくするため、依存関係が関心(DI)に注意する必要があります。 1)DIは、クラスを切り離すことにより、よりモジュール化されます。2)テストとコードの柔軟性の利便性を向上させ、3)DIコンテナを使用して複雑な依存関係を管理しますが、パフォーマンスの影響と円形の依存関係に注意してください。

はい、最適化されたAphPossibleandessention.1)CachingingusapCutoredatedAtabaseload.2)最適化、効率的なQueries、およびConnectionPooling.3)EnhcodeCodewithBultinctions、Avoididingglobalbariables、およびUsingopcodeching

keyStrategIestsoSificlyvoostphpappliceperformanceare:1)useopcodecachinglikeToreexecutiontime、2)最適化abaseの相互作用とプロペラインデックス、3)3)構成

aphpDependencyInjectionContaineriSATOULTAINATINAGECLASSDEPTINCIES、強化測定性、テスト可能性、および維持可能性。

SELECT DEPENTENCINGINOFCENT(DI)大規模なアプリケーションの場合、ServicElocatorは小さなプロジェクトまたはプロトタイプに適しています。 1)DIは、コンストラクターインジェクションを通じてコードのテスト可能性とモジュール性を改善します。 2)ServiceLocatorは、センター登録を通じてサービスを取得します。これは便利ですが、コードカップリングの増加につながる可能性があります。

phpapplicationscanbeoptimizedforspeedandEfficiencyby:1)enabingopcacheinphp.ini、2)PreparedStatementswithpordatabasequeriesを使用して、3)LoopswithArray_filterandarray_mapfordataprocessing、4)の構成ngincasaSearverseproxy、5)

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

Dreamweaver Mac版
ビジュアル Web 開発ツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません
