【问题标题】:Authentication request returned unexpected result: 404身份验证请求返回意外结果:404
【发布时间】:2015-08-26 04:51:24
【问题描述】:

异常详情:Google.GData.Client.GDataRequestException:执行身份验证请求返回意外结果:404

这是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    ds.Tables.Add("GmailContacts");
    ds.Tables[0].Columns.Add("EmailID");

    RequestSettings rs = new RequestSettings("Gmail", txtUserName.Value, txtPassword.Value);
    rs.AutoPaging = true;
    ContactsRequest cr = new ContactsRequest(rs);

    Feed<Contact> feed = cr.GetContacts();

    foreach (Contact contact in feed.Entries)
    {
        foreach (Email email in contact.Emails)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr["EmailID"] = email.Address.ToString();
            ds.Tables[0].Rows.Add(dr);
        }
    }
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

【问题讨论】:

标签: c# asp.net .net api


【解决方案1】:

检查我自己的解决方案

步骤:

  1. 转到https://console.developers.google.com/project 并创建项目。
  2. 选择项目,从左上角菜单中选择 APIs & auth。
  3. 选择凭据
  4. 使用按钮创建 OAuth 创建新客户端 ID(应用程序类型 - 已安装的应用程序。
  5. 填写产品名称
  6. 保存

之后,您获得了本机应用程序的客户端 ID: 客户编号, 客户秘密, 重定向 URI

从 NuGet 安装 Google.Apis.Auth

代码

string clientId = null; // https://console.developers.google.com/project/xxx
string clientSecret = null; // https://console.developers.google.com/project/xxx
string accessCode = null; // You will get this code after GetAccessToken method
string redirectUri = null; // https://console.developers.google.com/project/xxx
string applicationName = null; // https://console.developers.google.com/project/xxx
// Scopes https://support.google.com/a/answer/162106?hl=en
string scopes = null; // put your scope like https://www.google.com/m8/feeds/
string accessType = "offline";
string tokenType = "refresh";

OAuth2Parameters parameters = new OAuth2Parameters
{
    ClientId = clientId,
    ClientSecret = clientSecret,
    RedirectUri = redirectUri,
    Scope = scopes,
    AccessType = accessType,
    TokenType = tokenType
};

if (accessCode == null)
{
    string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
    // Start webbrowser
    Process.Start(url);

    // Load code from web via popup, etc.
    parameters.AccessCode = accessCodeFromWeb;
}

// Check accessToken and refreshToken
// After first acceess with  GetAccessToken you will get that information
if (accessToken == null || refreshToken == null)
{
    OAuthUtil.GetAccessToken(parameters);

    // Save yours accessToken and refreshToken for next connection
    accessToken = parameters.AccessToken;
    refreshToken = parameters.RefreshToken;
}
else
{   
    // Restore your token from config file, etc.
    parameters.AccessToken = accessToken;
    parameters.RefreshToken = refreshToken;
}

RequestSettings rs = new RequestSettings(applicationName, parameters);

return new ContactsRequest(rs);

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2019-09-03
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多