gpt4 book ai didi

oauth-2.0 - Oauth2 Yahoo Gemini API

转载 作者:行者123 更新时间:2023-12-02 03:25:01 25 4
gpt4 key购买 nike

我在调用 Yahoo Gemini API 以从我的 C# 控制台(桌面)应用程序访问 Yahoo Gemini Advertising 时遇到问题。

以下是我使用的步骤:

  1. https://developer.yahoo.com/apps/create/ 上创建已安装的应用程序。这给了我 {Client ID}{Client Secret}
  2. https://api.login.yahoo.com/oauth2/request_auth?client_id={客户端 ID} &redirect_uri=oob&response_type=code&language=zh-cn。这将带我到我登录的雅虎登录屏幕。按同意按钮,下一个屏幕显示七个字母的授权代码(比如 nzbcns9)。我记下这个授权码。
  3. 然后我使用以下代码尝试获取访问 token :

    class Program
    {
    static void Main(string[] args)
    {
    string clientId = {Client ID};
    string secret = {Client Secret};

    var request = WebRequest.Create(@"https://api.login.yahoo.com/oauth2/get_token");
    request.Method = "POST";

    SetBasicAuthHeader(request, clientId, secret);

    string postData = "grant_type=authorization_code&redirect_uri=oob&code=nzbcns9";
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] byte1 = encoding.GetBytes(postData);
    request.ContentLength = byte1.Length;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byte1, 0, byte1.Length);
    dataStream.Close();

    request.ContentType = "application/x-www-form-urlencoded";
    var response = request.GetResponse();
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    }

    static void SetBasicAuthHeader(WebRequest request, String userName, String userPassword)
    {
    string authInfo = userName + ":" + userPassword;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    request.Headers["Authorization"] = "Basic " + authInfo;
    }
    }

然后我得到

Unhandled Exception: System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse().

我做错了什么?

我也尝试使用 Fiddler 发布相同的消息,我明白了

{"error":"invalid_request"}

最佳答案

我试过你的代码,对我有用的是在 Stream dataStream = request.GetRequestStream ();

所以这有效:

    string postData = "grant_type=authorization_code&redirect_uri=oob&code=nzbcns9";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postData);
request.ContentLength = byte1.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byte1, 0, byte1.Length);
dataStream.Close();

关于oauth-2.0 - Oauth2 Yahoo Gemini API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30879448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com