【问题标题】:Azure MobileApp : InvokeApiAsync failing with InvalidOperationExceptionAzure MobileApp:InvokeApiAsync 失败并出现 InvalidOperationException
【发布时间】:2015-11-27 03:20:02
【问题描述】:

我正在试用 Azure 移动应用和最近发布的 SDK https://azure.microsoft.com/en-us/blog/azure-mobile-apps-november-2015-update/

我创建了服务,启动 Todo 应用程序运行良好。然后我添加了 facebook 身份验证,这也有效。我还想从服务中获取一些额外的 FB 信息,所以基于解决方案和 github 上的示例应用程序,我添加了一个新的 api

https://github.com/Azure/azure-mobile-apps-net-server/blob/2d2901ed5207f0ba6698660eb4ee568a63f18581/samples/SampleApp/Controllers/SecuredController.cs

服务器端代码

public class AuthenticationController : ApiController
{
    [Authorize]
    public async Task<JObject> GetIdentity()
    {
        FacebookCredentials fb = await this.User.GetAppServiceIdentityAsync<FacebookCredentials>(this.Request);
        var result = new JObject();
        if (fb != null)
        {
            var accessToken = fb.AccessToken;
            result.Add("facebook", await GetProviderInfo("https://graph.facebook.com/me?access_token=" + accessToken));
        }

        return result;
    }

    private async Task<JToken> GetProviderInfo(string url)
    {
        var c = new HttpClient();
        var resp = await c.GetAsync(url);
        resp.EnsureSuccessStatusCode();
        return JToken.Parse(await resp.Content.ReadAsStringAsync());
    }

}

Android 项目中的客户端代码

var user = await TodoItemManager.Instance.ClientInstance.LoginAsync(Forms.Context, MobileServiceAuthenticationProvider.Facebook);
var info = await TodoItemManager.Instance.ClientInstance.InvokeApiAsync("GetIdentity", null, HttpMethod.Get, null);

LoginAsync 成功,我得到了 UserId 和 Token,但 InvokeApiAsync 失败并出现以下异常

Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
  at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient+<ThrowInvalidResponse>d__18.MoveNext () [0x0022f] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
11-26 19:09:10.897 I/mono-stdout( 3514):   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient+<ThrowInvalidResponse>d__18.MoveNext () [0x0022f] in <filename unknown>:0 
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201 
11-26 19:09:10.898 I/mono-stdout( 3514): --- End of stack trace from previous location where exception was thrown ---
11-26 19:09:10.898 I/mono-stdout( 3514):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 
11-26 19:09:10.899 I/mono-stdout( 3514):   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201 
11-26 19:09:10.900 I/mono-stdout( 3514):   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:124 
  at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient+<SendRequestAsync>d__1d.MoveNext () [0x0010d] in <filename unknown>:0 

我错过了什么或做错了什么? 提前谢谢..

【问题讨论】:

  • 请只发布相关代码,不要链接到外部代码源。另外,请正确格式化错误信息。
  • 我认为当您调用 "GetIdentity" 时可能会丢失身份验证标头。您可以使用 Fiddler 来检查该值吗?我认为您可以将 Fiddler 配置为您的 Android 模拟器的代理。你的控制器上有[MobileAppController] 属性吗?
  • 要确认您是否缺少http请求标头上的身份验证令牌,您可以从`GetIdentity()`方法中删除[Authorize],然后重新部署您的服务,看看是否有相同的错误或其他错误。
  • 您在GetProviderInfo() 方法上有问题吗?你设置了什么权限?您可以使用“Graph API Explorer”developers.facebook.com/tools/explorer 生成具有您选择的权限的令牌。然后您可以使用该令牌测试您的代码。
  • 嘿迈克尔,感谢您的建议。我在控制器上有[MobileAppController] 属性。我什至尝试删除所有身份验证。基本上创建了一个新服务并添加了一个新的 ApiController 和一个 GetIdentity 方法,它只返回一个字符串并得到相同的 The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 错误。提琴手也没有太多运气。我通过提琴手代理了我的安卓设备。我看到所有应用程序的痕迹。但没有看到任何移动应用程序。

标签: azure azure-mobile-services


【解决方案1】:

我在使用新的 Azure 应用服务示例项目的项目中遇到了类似的错误。我只是尝试使用我自己的模型类从 Azure 数据库中检索数据,而不是像你一样进行身份验证。

这是错误:

Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: 您要查找的资源已被删除,有它的名字 已更改,或暂时不可用。

事实证明,在我的移动服务器项目中,我的模型定义为 CorporateEvent,而在我的客户端项目中,我的模型定义为 CorporateEventModel。一旦我将客户端项目中的模型重命名为CorporateEvent,错误就消失了,我就可以访问数据库了。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2019-08-15
    • 2020-04-10
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多