msgraph-sdk-go トレーニング サンプル コードを実行すると「アクセス トークンの取得が空です」エラーが発生する

WBOY
リリース: 2024-02-09 08:12:30
転載
594 人が閲覧しました

运行 msgraph-sdk-go 训练示例代码时出现“获取访问令牌为空”错误

php エディターの Xiaoxin は本日、msgraph-sdk-go トレーニング サンプル コードに関する質問をしました。実行中に、「アクセス トークンの取得が空です」エラーが発生する場合があります。このエラーにより、コードが正しく実行されず、トレーニング結果に影響を与える可能性があります。この記事では、サンプル コードをスムーズに実行し、より良いトレーニング エクスペリエンスを楽しむために、この問題の原因と解決策を詳しく紹介します。

質問の内容

ここから msgraph-sdk-go トレーニング コードを実行しようとすると: https://github.com/microsoftgraph/msgraph-training-go というメッセージが表示されます。 validauthenticationtokenmsg : グラフィックス API 呼び出しの実行時のアクセス トークンは空です。 試用のためにインスタント サンドボックスを使用して Microsoft 開発者アカウントを構成しました。 ここのチュートリアルで説明されているようにアプリケーション登録を作成し、アプリケーションに必要な権限を付与しました。 コードは apptoken を取得できますが、ユーザーを取得する呼び出しは上記のエラーで失敗します。ここで何かが足りないでしょうか? msgraph-training の例から次のコードを試しました

リーリー

チュートリアルの説明に従って、アプリケーションに

user.read.all

権限を追加しました。 ユーザーのリストを取得する代わりに、次のエラーが表示されます: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">func (g *graphhelper) initializegraphforappauth() error { clientid := os.getenv(&quot;client_id&quot;) tenantid := os.getenv(&quot;tenant_id&quot;) clientsecret := os.getenv(&quot;client_secret&quot;) credential, err := azidentity.newclientsecretcredential(tenantid, clientid, clientsecret, nil) if err != nil { return err } g.clientsecretcredential = credential // create an auth provider using the credential authprovider, err := auth.newazureidentityauthenticationproviderwithscopes(g.clientsecretcredential, []string{ &quot;https://graph.microsoft.com/.default&quot;, }) if err != nil { return err } // create a request adapter using the auth provider adapter, err := msgraphsdk.newgraphrequestadapter(authprovider) if err != nil { return err } // create a graph client using request adapter client := msgraphsdk.newgraphserviceclient(adapter) g.appclient = client return nil } // this part works, and i get the apptoken with required scope, once decoded. func (g *graphhelper) getapptoken() (*string, error) { token, err := g.clientsecretcredential.gettoken(context.background(), policy.tokenrequestoptions{ scopes: []string{ &quot;https://graph.microsoft.com/.default&quot;, }, }) if err != nil { return nil, err } fmt.println(&quot;expires on : &quot;, token.expireson) return &amp;token.token, nil } // the getusers function errors out func (g *graphhelper) getusers() (models.usercollectionresponseable, error) { var topvalue int32 = 25 query := users.usersrequestbuildergetqueryparameters{ // only request specific properties select: []string{&quot;displayname&quot;, &quot;id&quot;, &quot;mail&quot;}, // get at most 25 results top: &amp;topvalue, // sort by display name orderby: []string{&quot;displayname&quot;}, } resp, err := g.appclient.users(). get(context.background(), &amp;users.usersrequestbuildergetrequestconfiguration{ queryparameters: &amp;query, }) if err != nil { fmt.println(&quot;users.get got error&quot;, err.error(), resp) printodataerror(err) } resp, err = g.appclient.users(). get(context.background(), nil) if err != nil { fmt.println(&quot;users.get got error with nil&quot;, err.error(), resp) } return resp, err }</pre><div class="contentsignin">ログイン後にコピー</div></div>

回避策

試行錯誤の末、私にとってうまくいった修正は、例のバージョンが私が試していた実際のアプリケーションと一致しないということでした。 私が使用しているベータ版の msgraph アプリケーションは v0.49 で、msgraphsdk チュートリアルは v0.48 を使用しています。 go mod コマンドは最初に最新の v0.49 を選択しました。おそらく、msgraph-training の go.mod ファイルを確認した後、go.mod

ファイル v0.48 を使用します。 com/microsoftgraph/msgraph-training-go" rel="nofollow noreferrer">リポジトリ すべてが動作し始めます。 これが将来他の人にも役立つことを願っています。

以上がmsgraph-sdk-go トレーニング サンプル コードを実行すると「アクセス トークンの取得が空です」エラーが発生するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:stackoverflow.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!